jquery set all checkbox checked

前端 未结 3 1393
心在旅途
心在旅途 2021-01-11 11:28

I am using jquery 1.11.1 and this is my code:

$(\"#rowchkall\").change(function(){
    if($(this).is(\':checked\')){
        $(\"input:checkbox[class=rowchk]         


        
相关标签:
3条回答
  • 2021-01-11 11:46

    Use .prop() instead of .attr()

    $(this).prop('checked', true); //true to check else false uncheck
    

    Your code can be simplified as

    $("#rowchkall").change(function () {
        $("input:checkbox.rowchk").prop('checked',this.checked);
    });
    

    A Good Read .prop() vs .attr()

    0 讨论(0)
  • 2021-01-11 12:01

    Try to use .prop() instead of .attr()

    $("#rowchkall").change(function(){
      $("input:checkbox[class=rowchk]").prop('checked', this.checked);
    });
    

    And you don't need to iterate over all the elements to change its property.

    0 讨论(0)
  • 2021-01-11 12:12
    $("#rowchkall").change(function(){
        if($(this).is(':checked')){
            $("input.rowchk[type=checkbox]").each(function() {
                alert("set checked");
                $(this).attr('checked',true);
            });
        }else{
            $("input.rowchk[type=checkbox]").each(function() {
                $(this).attr('checked', false);
            });
        } 
    });
    
    0 讨论(0)
提交回复
热议问题