Javascript 选中该行的复选标记时启用表中的元素
我有一个HTML表格。想象一下这样的情况(我将使用伪代码):Javascript 选中该行的复选标记时启用表中的元素,javascript,jquery,html,Javascript,Jquery,Html,我有一个HTML表格。想象一下这样的情况(我将使用伪代码): 但是现在,因为它们都是内部标记,所以我不能使用该代码。有谁能告诉我我可以做些什么来更改此代码,使其与表一起工作吗?他们不再是兄弟姐妹了。因此,必须从一个公共父级遍历每个父级 $('[name*=chkd]').click(function(){ if ($(this).is(':checked')) { $(this).closest('tr').find('select').removeAtt
但是现在,因为它们都是内部标记,所以我不能使用该代码。有谁能告诉我我可以做些什么来更改此代码,使其与表一起工作吗?他们不再是兄弟姐妹了。因此,必须从一个公共父级遍历每个父级
$('[name*=chkd]').click(function(){
if ($(this).is(':checked')) {
$(this).closest('tr').find('select').removeAttr('disabled');
}
else{
$(this).closest('tr').find('select').attr('disabled', 'disabled');
}
});
假设您的标记如下所示:
<table>
<tr>
<td>
<input type="checkbox" />
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
</tr>
</table>
参考资料:
输入的
不再是选择的同级,因此您需要找到父级的同级,然后在这些元素中找到选择的同级
$('[name*=chkd]')。单击(函数(){
var$sides=$(this.parent().sides().find('select');
如果($(this).is(':checked')){
$sides.removeAttr('disabled');
}否则{
$sides.attr('disabled','disabled');
}
});代码>
asdf
asdf
asdf
asdf
您可以使用find
搜索选定元素,并将禁用属性指定给复选框值:
$('.myTable').on('change', ':checkbox', function() {
var $controls = $(this).closest('tr').find('select').prop('disabled', this.checked);
if (this.checked) {
$controls.val(0)
}
}).find(':checkbox').change();
您可以检查可能的副本
<table>
<tr>
<td>
<input type="checkbox" />
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="checkbox" />
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
<td>
<select disabled>
<option>A</option>
<option>B</option>
</select>
</td>
</tr>
</table>
$(function() {
// When a checkbox is clicked.
$("input[type=checkbox]").on("click", function () {
// Is it checked?
var checked = $(this).is(":checked");
// Look for the selects in the row and toggle their disabled property.
$(this).closest("tr").find("select").prop("disabled", !checked);
});
});
$('.myTable').on('change', ':checkbox', function() {
var $controls = $(this).closest('tr').find('select').prop('disabled', this.checked);
if (this.checked) {
$controls.val(0)
}
}).find(':checkbox').change();