jq 全选和反选以及判断那条被选中

<body>
<div><input type="checkbox" id="a" />全选</div>
<div><input type="checkbox" class="b" value="aa" />aa</div>
<div><input type="checkbox" class="b" value="bb"  />bb</div>
<div><input type="checkbox" class="b" value="cc"  />cc</div>
<div><input type="button" id="d" value="获取" /></div>
</body>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#a").click(function(){
        
        var xz = $(this).prop("checked");  //判断a是否被选中
        
        $(".b").prop("checked",xz);  //让b的属性与a一样
        
    })
    $("#d").click(function(){
        var attr=new Array();  //建立一个数组
        $(".b").each(function(){  //each循环遍历b
            if($(this).prop("checked"))  //判断被循环的这一条是否被选中
            {
                   attr.push($(this).val());  //把被选中的数据添加到数组
            }
        });
        alert(attr);
        
    })
});
</script>
</html>

原文地址:https://www.cnblogs.com/bilibiliganbei/p/6019192.html