Stack Overflow

This question already has an answer here:

  • Setting “checked” for a checkbox with jQuery? 38 answers

  • You're using the wrong method, you should be using prop

    $(document).ready(function() {
        $('#CHECK-ALL').click(function() {
            if ($(this).is(':checked')) {
                $('#P').prop('checked',true);                
            } else {
                $('#P').prop('checked',false);                
            }
        });
    });
    

    Your code could be reduced to

    $('#CHECK-ALL').click(function() {
        $('#P').prop('checked', this.checked);                
    });
    

    When you set the attribute, it works fine, but it doesn't change the property, so the next time you check $(this).is(':checked') it's still unchecked, as that checks the property, not the attribute, which is why it's not working.

    链接地址: http://www.djcxy.com/p/12266.html

    上一篇: 取消选中复选框

    下一篇: 堆栈溢出