checkbox全选与全不选

来源:互联网 发布:第三次网络机器人大战 编辑:程序博客网 时间:2024/05/22 01:26

html部分代码:

<table>
<tr>
<td><input type="checkbox" name="b">全选</td><td>内容</td>
</tr>

<?php for($i=0;$i<4;$i++) { ?>
<tr>
<td><input type="checkbox" name="a[]"></td><td>复选<?php echo $i;?></td>
</tr>
<?php } ?>
</table>

jQuery部分代码:


$("input[name='b']").click(function(){
//判断当前点击的复选框处于什么状态$(this).is(":checked") 返回的是布尔类型
if($(this).is(":checked")){
$("input[name='a[]']").prop("checked",true);
}else{
$("input[name='a[]']").prop("checked",false);
}
});

$("input[name='a[]']").click(function(){
var all = 0;
var checked_no = 0;

$("input[name='a[]']").each(function(){
if ($(this).prop("checked")) {
checked_no++;
}

all++;
});

if (checked_no == all) {
$("input[name='b']").prop("checked", true);
} else {
$("input[name='b']").prop("checked", false);
}
});
</script>
0 0
原创粉丝点击