I have modal popup window which open on button click for each row in data table. On modal window I have 4 dropdowns and out of these I want one dropdown to be disabled for every row data. When user selects the radio button value as "Yes" it should be enabled.
Here is my script:
$(document).ready(function(){
$('.action').attr('disabled', 'disabled');
var $checkBox = $('.check');
$checkBox.on('change', function(e) {
//get the previous element to us, which is the select
var $select = $(this).prev();
if (this.checked) {
$select.removeAttr('disabled');
} else {
$select.attr('disabled', 'disabled');
}
});
});
This is my html:
<div class="form-group">
<label>Action Taken:</label>
<input type="radio" class="check" id="check" name="check" value="Yes">Yes
<input type="radio" class="check" id="check" name="check" value="Yes">No
<select name="action" id="action" class="form-control" disabled>
<option value="">Select Action</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
If I am adding the radio buttons after select tag its’s working for one row, but after submit when I am selecting second row data it stops working then I have to refresh page again, but I want radio button selection before the select tag also it should work for each row. Can someone help me out what I am doing wrong here
2
Answers
first of all you need 2 different values for your radio button , you have
Yes
for both buttonsyou can put your radio buttons and select in a parent div and use that parent to get to access to selectbox instead of using
next
,prev
… like<div class="form-group">
https://jsfiddle.net/ns5g3rqe/
There’s several issues here, and each need to be corrected for your code to work properly:
.action
is a class selector, yet the select has thatid
. As your question suggests there may be multiple clones of this HTML, remove theid
on theselect
and use theclass
insteadselect
is not theprev()
element to either radio. Fix this by retrieving theclosest()
common parent andfind()
from there.id
of#check
on the radio buttons, when they must be unique. Either change or remove them.With that corrected, the code works: