jQuery使用prop设置checkbox全选、反选

时间:2023-01-27 20:15:38

$(function(){

    var checkbox = $("input[type='checkbox']");

    //全选

    $('#select-all').click(function(){

        checkbox.prop("checked", true);

    });

    //反选

    $('#select-reverse').click(function(){

        checkbox.prop("checked", function(index, attr){

            return !attr;

        });

    });

});

$(document).ready(function(){

            $("#allsel").click(function(){

        

            //prop全选

            var is =$(this).prop("checked");

            $("table :checkbox").prop("checked",is);

            

            });

            

            $("table :checkbox").click(function(){

                $("#allsel").prop("checked",true);

                $("table :checkbox").each(function(){

                    var is = $(this).prop("checked");

                    if(is==false){

                    $("#allsel").prop("checked",false);

                    }

                });

            });

            

            //prop反选

            $("#notsel").click(function(){

                $("table :checkbox").prop("checked",function(i,val){

                    return !val;

                

                });

            

            });

        });