我有一堆这样的复选框。如果选中“检查我”复选框,则应启用所有其他 3 个复选框,否则应禁用它们。我如何使用 jQuery 执行此操作?
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9">Check Me
<input type="checkbox" name="chk9[120]">
<input type="checkbox" name="chk9[140]">
<input type="checkbox" name="chk9[150]">
</form>
最佳答案
稍微更改您的标记:
$(function() {
enable_cb();
$("#group1").click(enable_cb);
});
function enable_cb() {
if (this.checked) {
$("input.group1").removeAttr("disabled");
} else {
$("input.group1").attr("disabled", true);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="group1">Check Me <br>
<input type="checkbox" name="chk9[120]" class="group1"><br>
<input type="checkbox" name="chk9[140]" class="group1"><br>
<input type="checkbox" name="chk9[150]" class="group1"><br>
</form>
您可以在不引入 ID 和类的情况下使用属性选择器来完成此操作,但速度较慢且(恕我直言)更难阅读。
关于jQuery - 复选框启用/禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2330209/