简体   繁体   English

jQuery Uncheck复选框不起作用

[英]jQuery Uncheck checkbox not working

After trying multiple examples on StackOverflow to fix my issue, I still can't figure out my problem. 在StackOverflow上尝试了多个示例来解决我的问题后,我仍然无法弄清我的问题。

I have 3 checkboxes: 我有3个复选框:

<input id="pets_dog" class="text" type="checkbox" class="pets" name="pets[]" value="pets_dog"> Dogs</input>
<input id="pets_cats" class="text" type="checkbox" class="pets" name="pets[]" value="pets_cats"> Cats</input>
<input id="pets_none" class="text" type="checkbox" class="pets" name="pets[]" value="pets_no"> None</input>

And when "pets_none" is checked, I need the other 2 checkboxes to be unchecked and vice versa. 当选中“ pets_none”时,我需要取消选中其他2个复选框,反之亦然。 Here is my current jQuery code: 这是我当前的jQuery代码:

        $("#pets_none").change(function() {
            if($("#pets_none").attr('checked')) {
            $("#pets_cats").removeAttr('checked');
            $("#pets_dogs").removeAttr('checked');
            } 
        });

I can't figure out for the life of me why it's not working, and appreciate any help. 我一生都无法弄清为什么它不起作用,并感谢您的帮助。

Your html should be 您的html应该是

<input id="pets_dog" class="text pets" type="checkbox" name="pets[]" value="pets_dog" /> Dogs
<input id="pets_cats" class="text pets" type="checkbox" name="pets[]" value="pets_cats" /> Cats
<input id="pets_none" class="text pets" type="checkbox" name="pets[]" value="pets_no" /> None​​​​​​​​​​​

JS JS

$(function(){
   $("#pets_none").on('change', function(e) {
       if($(this).is(':checked')) {
           $("#pets_dog").attr('checked', false);
           $("#pets_cats").attr('checked', false);
       } 
   });
});

DEMO. 演示。

Update : You have used class attribute twice, it should be as follows 更新:您已两次使用class属性,应如下所示

<input id="pets_dog" type="checkbox" class="pets text" name="pets[]" value="pets_dog" /> Dogs
<input id="pets_cats" type="checkbox" class="pets text" name="pets[]" value="pets_cats" /> Cats
<input id="pets_none" type="checkbox" class="text pets" name="pets[]" value="pets_no" /> None​

JS JS

$(function(){
    $(".pets").on('change', function(e) {
        var el=$(this);
        if(el.is(':checked') && el.attr('id')=='pets_none') {
            $(".pets").not(el).attr('checked', false);
        }
        else if(el.is(':checked') && el.attr('id')!='pets_none')
        {
            $("#pets_none").attr('checked', false);
        }  
    });
});

DEMO. 演示。

You can use the prop method: 您可以使用prop方法:

 $("#pets_none").change(function() {
     if (this.checked) {
        $("#pets_cats, #pets_dog").prop('checked', false);
     } 
 });

Use the .prop() method: 使用.prop()方法:

...

$("#pets_cats").prop('checked', false);
$("#pets_dogs").prop('checked', false);

...

And to check whether the #pets_none is checked, use that method too: 要检查是否已检查#pets_none ,也可以使用该方法:

$("#pets_none").prop('checked');
var checked = $(this).find('Enable').text().toLowerCase();
$("#chk__Enable").each(function () {
    if (checked == 'true') {
        $(this).attr('checked', 'checked');
    } else {
        $(this).removeAttr('checked');
    }
});

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

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