jQuery实现复选框全选、不全选

JS脚本:

$(function(){
    $("#delAll").click(function(){
        $(":input[name=del]").attr("checked",this.checked)
    })
    
    $(":checkbox[name=del]").click(function(){
        var varSize = $(":checkbox[name=del]").size();
        var length = 0;
        $(":checkbox[name=del]").each(function(){
            if($(this).attr("checked")){
                length++;
            }
        });
        if(varSize==length){
            $("#delAll").attr("checked",true)
        }else{
            $("#delAll").attr("checked",false)
        }
            
    })
    
})

html:

<input type="checkbox" name="delAll" id="delAll"/>全选
    <br>
    <input type="checkbox" name="del" />1
    <input type="checkbox" name="del" />2
    <input type="checkbox" name="del" />3
    <input type="checkbox" name="del" />4

猜你喜欢

转载自blog.csdn.net/qq_31973655/article/details/81386717