我已经尝试禁用它option
。但是当它已经选择时它没有。select
selected
disable
下面是query
for select option
,可以创建dynamically
。
var count = 0;
$(document).on('click', '#add', function () {
count++;
var html = '';
html += '<tr id="trrows">';
html += '<td id="medicinename"> <select name="med_name[]" id="med_name[]" class="form-control med_name" ><option value=""> Select Medicine</option> <?php echo fill_select_box($conn, "0"); ?></select></td>';
html += '<td id="mor"> <input type="text" name="morning[]" id="morning[]" class="form-control morning" /> </td>';
html += '<td id="noo"> <input type="text" name="noon[]" id="noon[]" class="form-control noon" /> </td>';
html += '<td id="nigh"> <input type="text" name="night[]" id="night[]" class="form-control night" /> </td>';
html += '<td id="period"> <input type="text" name="period[]" id="period[]" class="form-control period" /> </td>';
html += '<td> <button type="button" name="remove" id="remove" class="btn btn-danger btn-xs remove" > - </button> </td>';
html += '</tr>';
$('#rows').append(html);
});
$(document).on('click', '.remove', function () {
$(this).closest("#trrows").remove();
});
下面是query
fordisable
上的选择选项(med_name[])
,
$('select[name*="med_name[]"]').change(function(){
$('select[name*="med_name[]"] option').attr('disabled',false);
$('select[name*="med_name[]"]').each(function(){
var $this = $(this);
$('select[name*="med_name[]"]').not($this).find('option').each(function(){
if($(this).attr('value') == $this.val())
$(this).attr('disabled',true);
});
});
});
下面是HTML,
<table class="table table-bordered mb-0" id="medical">
<thead>
<tr>
<th>Medicine Name</th>
<th>Morning</th>
<th>Noon</th>
<th>Night</th>
<th>Period</th>
</tr>
</thead>
<tbody id="rows">
</tbody>
</table>
<br><br>
<div align="right">
<button type="button" name="add" id="add" class="btn btn-success btn xs"> + </button>
</div>
我不知道我哪里错了。我怎样才能做到这一点?任何帮助都将受到高度赞赏。