全选

<body>
<input type="checkbox" id="ckAll" />全选<br />
<input type="checkbox" name="sub" />1<br />
<input type="checkbox" name="sub"/>2<br />
<input type="checkbox" name="sub"/>3<br />
<input type="checkbox" name="sub"/>4<br />
<script type="text/javascript">
$(function(){
$('#ckAll').click(function(){
$('input[name=sub]').prop('checked',this.checked);
})
var len=$('input[name=sub]').length;
$('input[name=sub]').click(function(){
var lens=$('input[name=sub]:checked').length;
if(lens==len){
$('#ckAll').prop('checked',true);
}else{
$('#ckAll').prop('checked',false);
}
})
})

</script>

<script>
/*
$(function(){
$("#ckAll").click(function() {
$("input[name='sub']").prop("checked",this.checked))};
$("input[name='sub']").click(function() {
var $subs = $("input[name='sub']");
$("#ckAll").prop("checked" , $subs.length == $subs.filter(":checked").length ? true :false);
});    
})
*/
</script>    
</body>
View Code
原文地址:https://www.cnblogs.com/wangmeiMeo/p/7122175.html