简体   繁体   English

根据一个复选框的状态禁用复选框

[英]Disable checkboxes based on status of one checkbox

if 'choose not to answer' is checked, i need to disable the other checkboxes, here is my code and html, any help as to why it's not working? 如果选中了“选择不回答”,则需要禁用其他复选框,这是我的代码和html,有关为什么它不起作用的任何帮助?

code

function toggleStatus() {
if ($('#toggleElement').is(':checked')) {
    $('#living :input').attr('disabled', true);
} else {
    $('#living :input').removeAttr('disabled');
}

html 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>

I don't see your function being called anywhere, you can rig it up inside a document.ready handler though, and simplify it too, like this: 我看不到您的函数在任何地方都可以调用,但是您可以将其安装在document.ready处理程序中,并进行简化,如下所示:

$(function() {
  $("#toggleElement").change(function() {
    $('input[name=living]').not(this).attr('disabled', this.checked);
  });
});

Also note that your selector was incorrect, it should be just based on the name...and that ID should be left out, since it's duplicated. 还要注意,您的选择器不正确,应该仅基于名称...并且应该删除ID,因为它是重复的。

Try this instead: 尝试以下方法:

if ($('#toggleElement').is(':checked')) {
    $('#living :input').attr('disabled', "disabled");
} else {
    $('#living :input').attr('disabled', false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM