jquery select all checkboxes

前端 未结 19 2728
情书的邮戳
情书的邮戳 2020-12-13 12:36

I have a series of checkboxes that are loaded 100 at a time via ajax.

I need this jquery to allow me to have a button when pushed check all on screen. If more are lo

19条回答
  •  抹茶落季
    2020-12-13 13:19

    A very simple check/uncheck all without the need of loop

     Check / Uncheck All
    
     Option 1
     Option 2
     Option 3
    

    And the javascript (jQuery) accounting for "undefined" on checkbox value

    ** UPDATE - using .prop() **

    $("#checkAll").change(function(){
        var status = $(this).is(":checked") ? true : false;
        $(".chk").prop("checked",status);
    });
    

    ** Previous Suggestion - may not work **

    $("#checkAll").change(function(){
        var status = $(this).attr("checked") ? "checked" : false;
        $(".chk").attr("checked",status);
    });
    

    OR with the suggestion from the next post using .prop() combined into a single line

    $("#checkAll").change(function(){
        $(".chk").attr("checked",$(this).prop("checked"));
    });
    

提交回复
热议问题