最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Uncheck checkbox not working - Stack Overflow

programmeradmin0浏览0评论

After trying multiple examples on StackOverflow to fix my issue, I still can't figure out my problem.

I have 3 checkboxes:

<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. Here is my current jQuery code:

        $("#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.

After trying multiple examples on StackOverflow to fix my issue, I still can't figure out my problem.

I have 3 checkboxes:

<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. Here is my current jQuery code:

        $("#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.

Share Improve this question edited Jun 16, 2012 at 19:41 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Jun 16, 2012 at 19:33 stoopkid1stoopkid1 1141 gold badge1 silver badge9 bronze badges 1
  • 5 You should not have 2 class attributes assigned as that is invalid Html. Having 2 class attributes also prevents you for example from selecting your inputs by class of pets. I.e: $(.pets) won't work as .text takes precedence. If you want 2 classes assigned do it like this class="text pets" – Nope Commented Jun 16, 2012 at 20:05
Add a comment  | 

4 Answers 4

Reset to default 6

Your html should be

<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

$(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

<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

$(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:

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

Use the .prop() method:

...

$("#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").prop('checked');
var checked = $(this).find('Enable').text().toLowerCase();
$("#chk__Enable").each(function () {
    if (checked == 'true') {
        $(this).attr('checked', 'checked');
    } else {
        $(this).removeAttr('checked');
    }
});
发布评论

评论列表(0)

  1. 暂无评论