Jquery 选中包含选定选项的表格行
我有一个包含许多行的表,我希望如果我在给定行的选择框中选择一个值,则选中该行中的复选框,如果选择框具有空值/无值,则该行中的复选框也将被取消选中 我目前的工作Jquery 选中包含选定选项的表格行,jquery,Jquery,我有一个包含许多行的表,我希望如果我在给定行的选择框中选择一个值,则选中该行中的复选框,如果选择框具有空值/无值,则该行中的复选框也将被取消选中 我目前的工作 $('#the_worksheettable .selectbox_1').change(function(){ value =$(this).val(); if(value !==''){ $('.checkbox_1').prop('checked',t
$('#the_worksheettable .selectbox_1').change(function(){
value =$(this).val();
if(value !==''){
$('.checkbox_1').prop('checked',true);
}else{
$('.checkbox_1').prop('checked',false);
}
});
<table class="tg" id="the_worksheettable">
<tr>
<th class="tg-031e">ID</th>
<th class="tg-031e">TEST NAME</th>
<th class="tg-031e">TEST NAME / MOLECULE</th>
<th class="tg-031e">SELECTOR</th>
</tr>
<tbody>
<tr>
<td class="tg-031e">1</td>
<td class="tg-ugh9">Capsule with Acid and buffer</td>
<td><select name="molecule[]" id="" class="selectbox_1">
<option value="">-Select-</option>
<option value="Uniformity">Uniformity</option>
<option value="Relative Density">Relative Density</option>
<option value="Trimethoprim">Trimethoprim</option>
</select></td>
<td class="tg-031e"><input type="checkbox" value="Capsule_with_Acid_and_buffer"name="test_names[]" class="checkbox_1"/></td>
</tr>
<tr>
<td class="tg-031e">1</td>
<td class="tg-ugh9">Capsule with Acid and buffer</td>
<td><select name="molecule[]" id="" class="selectbox_1">
<option value="">-Select-</option>
<option value="Uniformity">Uniformity</option>
<option value="Relative Density">Relative Density</option>
<option value="Sulfamethoxazole">Sulfamethoxazole</option>
</select></td>
<td class="tg-031e"><input type="checkbox" value="Capsule_with_Acid_and_buffer"name="test_names[]" class="checkbox_1"/></td>
</tr>
<tr>
<td class="tg-031e">2</td>
<td class="tg-ugh9">Uniformity of Weight</td>
<td><select name="molecule[]" id="" class="selectbox_1">
<option value="">-Select-</option>
<option value="Uniformity">Uniformity</option>
<option value="Relative Density">Relative Density</option>
<option value="Trimethoprim">Trimethoprim</option>
</select></td>
<td class="tg-031e"><input type="checkbox" value="Uniformity_of_Weight"name="test_names[]" class="checkbox_1"/></td>
</tr>
<tr>
<td class="tg-031e">2</td>
<td class="tg-ugh9">Uniformity of Weight</td>
<td><select name="molecule[]" id="" class="selectbox_1">
<option value="">-Select-</option>
<option value="Uniformity">Uniformity</option>
<option value="Relative Density">Relative Density</option>
<option value="Sulfamethoxazole">Sulfamethoxazole</option>
</select></td>
<td class="tg-031e"><input type="checkbox" value="Uniformity_of_Weight"name="test_names[]" class="checkbox_1"/></td>
</tr>
$('#工作表表格。选择框1')。更改(函数(){
value=$(this.val();
如果(值!=''){
$('.checkbox_1').prop('checked',true);
}否则{
$('.checkbox_1').prop('checked',false);
}
});
身份证件
测试名称
测试名称/分子
选择器
1.
酸缓冲胶囊
-挑选-
一致性
相对密度
甲氧苄啶
1.
酸缓冲胶囊
-挑选-
一致性
相对密度
磺胺甲恶唑
2.
重量均匀性
-挑选-
一致性
相对密度
甲氧苄啶
2.
重量均匀性
-挑选-
一致性
相对密度
磺胺甲恶唑
下面应该可以做到这一点
$('#the_worksheettable .selectbox_1').change(function(){
var value = $(this).val();
if(value !== ''){
$(this).closest('tr').find('.checkbox_1').prop('checked',true);
}else{
$(this).closest('tr').find('.checkbox_1').prop('checked',false);
}
});
您只需将当前选择所在的同一tr中的复选框作为目标:
$('#the_worksheettable .selectbox_1').change(function(){
$(this).closest('tr').find('.checkbox_1').prop('checked', $(this).val()!=="");
});
如果您也可以共享html,那就太好了。(只要一个示例行就可以了。)为了确保我读的正确,如果您单击行中的任何单元格,那么您希望切换复选框?“…如果我在给定行的选择框中选择一个值…”这不起作用,因为select将位于td元素内
.parent('tr')
将不返回任何对象。这是演示@MilindantWar Right。换了。记住错了,忘记了。parent()
只升了一级。答案很好,但我必须问,为什么要添加?true:false这是一个布尔值:$(this).val()==""