check/uncheck all checkboxes

后端 未结 4 1653
别跟我提以往
别跟我提以往 2021-01-21 02:45

I am trying to have a checkbox that checks/unchecks all the other checkboxes.

I am using this code:

$(\"#checkall\").toggle(   
    function () {
          


        
相关标签:
4条回答
  • 2021-01-21 03:27

    You can try with this

    $("#checkall").click(function() {
       $(".kselItems").prop('checked', this.checked);
    });
    
    0 讨论(0)
  • 2021-01-21 03:43

    Keep it simple. Try this

    $("#checkall").click(function() {
        $(".kselItems").attr('checked', this.checked);
    });
    

    Demo: http://jsfiddle.net/naveen/azkPR/

    0 讨论(0)
  • 2021-01-21 03:44

    Try filtering out the checkbox in your selectors.

            $("#checkall").toggle(   
                function () {
                    $(".kselItems:not(#checkall)").attr('checked', 'checked');
                },
                function () {
                    $(".kselItems:not(#checkall)").removeAttr("checked"); 
            });
    
    0 讨论(0)
  • 2021-01-21 03:48

    edit: while I writed, xeno06 answered too :)

    when you click "checkall", it 1) set the value to checked, and then 2) call the toggle function, which will find "checkall", and toogle it back.

    best way is to not put the ".kselItems" class to "checkAll" or, if "checkall" is inside ".ksleItems" use

    $(".kselItems not(#checkall)").(...)
    

    or

    $(".kselItems").not("#checkall").(...)
    

    for clarity I would use naveen solution

    $("#checkall").click(function() {
        $(".kselItems :checkbox").not("#checkall").attr('checked', this.checked);
    });
    
    0 讨论(0)
提交回复
热议问题