如果选中“选择不回答”,我需要禁用其他复选框,这是我的代码和 html,关于它为什么不起作用的任何帮助吗?
代码
function toggleStatus() {
if ($('#toggleElement').is(':checked')) {
$('#living :input').attr('disabled', true);
} else {
$('#living :input').removeAttr('disabled');
}
html
<p>With whom do you live? Choose all that apply<br/>
<input type="checkbox" name="living" value="alone" id="living">
Live alone <br>
<input type="checkbox" name="living" value="husband" id="living">
Husband <br>
<input type="checkbox" name="living" value="partner" id="living">
Partner <br>
<input type="checkbox" name="living" value="children" id="living">
Children <br>
<input type="checkbox" name="living" value="parents" id="living">
Parents <br>
<input type="checkbox" name="living" value="other_relatives" id="living">
Other relatives <br>
<input type="checkbox" name="living" value="religion" id="living">
Religious order <br>
<input type="checkbox" name="living" value="no_answer"
id="toggleElement">
Choose not to answer <br>
</p>
最佳答案
我没有看到你的函数在任何地方被调用,不过你可以在 document.ready
处理程序中安装它,并简化它,如下所示:
$(function() {
$("#toggleElement").change(function() {
$('input[name=living]').not(this).attr('disabled', this.checked);
});
});
另请注意,您的选择器不正确,它应该仅基于名称...并且应该省略该 ID,因为它是重复的。
关于javascript - 根据一个复选框的状态禁用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4444554/