<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>jQuery实现CheckBox全选、全不选</title>
<script src="js/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
checkboxAll = {
selectall:function(a, b) {
//a表示全选name值,b表示子checkbox的name值
var $select_all = $("input[name='" + a + "']");
var $childBox = $("input[name='" + b + "']");
for (var i = 0; i < $select_all.length; i++) {
$select_all[i].onclick = function() {
var checkTure = $(this).prop("checked");
var l = $("input[type='checkbox']").length;
for (var j = 0; j <= l; j++) {
var childAll = $("input[type='checkbox']").eq(j);
if (checkTure) {
childAll.prop("checked", true);
} else {
childAll.prop("checked", false);
}
}
};
}
for (var i = 0; i < $childBox.length; i++) {
$childBox[i].onclick = function() {
var checkTure = $(this).prop("checked");
//判断点击商品选中状态
var childlen = $("input[name='" + b + "']:checked").length;
//判断商品是否选中个数
if (childlen != $childBox.length) {
//判断全选下面的商品是否全部选中
$(this).prop("checked", false);
for (var m = 0; m <= $select_all.length; m++) {
//取消全选样式
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", false);
}
} else {
//点击商品选中
$(this).prop("checked", true);
var childlen = $("input[name='" + b + "']:checked").length;
//判断商品是否选中个数
if (childlen != $childBox.length) {
//判断商品是否全部选中
for (var m = 0; m <= $select_all.length; m++) {
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", false);
}
} else {
for (var m = 0; m <= $select_all.length; m++) {
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", true);
}
}
}
if (checkTure) {
//控制本身是否选中
$(this).prop("checked", true);
} else {
$(this).prop("checked", false);
}
};
}
}
};
checkboxAll.selectall("all", "childBox");
});
</script>
</head>
<body>
<div style="width: 100%;">
<div style="width: 300px; margin: 100px auto; border: 1px solid #ccc; padding: 20px;">
<input id="checkAll" name="all" type="checkbox" />全选
<br />
<input name="childBox" type="checkbox" />项1
<br />
<input name="childBox" type="checkbox" />项2
<br />
<input name="childBox" type="checkbox" />项3
<br />
<input name="childBox" type="checkbox" />项4
<br />
<input id="checkAll0" name="all" type="checkbox" />全选
</div>
</div>
</body>
</html>
<html>
<head>
<meta charset="UTF-8" />
<title>jQuery实现CheckBox全选、全不选</title>
<script src="js/jquery-1.11.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
checkboxAll = {
selectall:function(a, b) {
//a表示全选name值,b表示子checkbox的name值
var $select_all = $("input[name='" + a + "']");
var $childBox = $("input[name='" + b + "']");
for (var i = 0; i < $select_all.length; i++) {
$select_all[i].onclick = function() {
var checkTure = $(this).prop("checked");
var l = $("input[type='checkbox']").length;
for (var j = 0; j <= l; j++) {
var childAll = $("input[type='checkbox']").eq(j);
if (checkTure) {
childAll.prop("checked", true);
} else {
childAll.prop("checked", false);
}
}
};
}
for (var i = 0; i < $childBox.length; i++) {
$childBox[i].onclick = function() {
var checkTure = $(this).prop("checked");
//判断点击商品选中状态
var childlen = $("input[name='" + b + "']:checked").length;
//判断商品是否选中个数
if (childlen != $childBox.length) {
//判断全选下面的商品是否全部选中
$(this).prop("checked", false);
for (var m = 0; m <= $select_all.length; m++) {
//取消全选样式
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", false);
}
} else {
//点击商品选中
$(this).prop("checked", true);
var childlen = $("input[name='" + b + "']:checked").length;
//判断商品是否选中个数
if (childlen != $childBox.length) {
//判断商品是否全部选中
for (var m = 0; m <= $select_all.length; m++) {
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", false);
}
} else {
for (var m = 0; m <= $select_all.length; m++) {
var notSelect = $("input[name='" + a + "']").eq(m);
notSelect.prop("checked", true);
}
}
}
if (checkTure) {
//控制本身是否选中
$(this).prop("checked", true);
} else {
$(this).prop("checked", false);
}
};
}
}
};
checkboxAll.selectall("all", "childBox");
});
</script>
</head>
<body>
<div style="width: 100%;">
<div style="width: 300px; margin: 100px auto; border: 1px solid #ccc; padding: 20px;">
<input id="checkAll" name="all" type="checkbox" />全选
<br />
<input name="childBox" type="checkbox" />项1
<br />
<input name="childBox" type="checkbox" />项2
<br />
<input name="childBox" type="checkbox" />项3
<br />
<input name="childBox" type="checkbox" />项4
<br />
<input id="checkAll0" name="all" type="checkbox" />全选
</div>
</div>
</body>
</html>