How to clear all checkbox When checkbox id=“checkAll” are checked?

后端 未结 4 1862
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 23:04

How to clear all checkbox When checkbox id=\"checkAll\" are checked ?

in my demo , when user checked checkbox id=\"checkAll\" all checkbox are

相关标签:
4条回答
  • 2021-01-25 23:27

    If I'm understanding you correctly, you want to clear all the items when they click "Check All" - you might want to change the name to "Clear All" for clarity.

    If so, just add a simple ! in front of this.checked

    $('#checkAll').click(function () {
    $('input:checkbox').prop('checked', !this.checked); });

    0 讨论(0)
  • 2021-01-25 23:31

    You shouldn't use the same ID for multiple elements, since IDs are unique. Instead use class="checkedItem".

    If you want to clear all use

    $('#checkAll').click(function () {    
        $('input:checkbox').prop('checked', !this.checked);       
    });
    

    However this also clears the first checkbox. To prevent this use

    $('#checkAll').click(function () {    
        $('input:checkbox').not(this).prop('checked', !this.checked);       
    });
    
    0 讨论(0)
  • 2021-01-25 23:36

    You can use change() function and it you can check by using this.checked inside the change event, it will return boolean value for checkbox checked status :

    Check All on Main Checked:

    $('#checkAll').change(function () {    
       $('input:checkbox').prop('checked', this.checked); // all checked on check of #checkAll 
     });
    

    Uncheck All on Main Checked:

    $('#checkAll').change(function () {    
        $('input:checkbox').prop('checked', !this.checked);   // all unchecked on check of #checkAll
         });
    

    if you want all unchecked but no the main check box then:

    Uncheck All Excluding the Main:

    $('#checkAll').change(function () {    
        $('input:checkbox').not(this).prop('checked', !this.checked);   // all unchecked on check of #checkAll but not itself
             });
    
    0 讨论(0)
  • 2021-01-25 23:42

    Just apply the negation of this.checked

    $('#checkAll').change(function () {    
         $('input:checkbox').not(this).prop('checked', !this.checked);       
     });
    

    Demo

    0 讨论(0)
提交回复
热议问题