第一种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js">
</script>
<script type="text/javascript">
$(function() {
$("#allchekc").change(function() {
$(".itema").prop("checked", $(this).prop("checked"));
});
$(".itema").change(function() {
if(!$(this).prop("checked")) {
$("#allchekc").prop("checked", false);
}
if($(".itema:checked").length == $(".itema").length) {
$("#allchekc").prop("checked", true);
}
});
});
</script>
</head>
<body>
<input type="checkbox" id="allchekc" />全选
<input type="checkbox" class="itema" />选项1
<input type="checkbox" class="itema" />选项2
<input type="checkbox" class="itema" />选项3
</body>
</html>
第二种方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function() {
$("#allchekc").change(function() {
$(".itema").prop("checked", $(this).prop("checked"));
});
$(".itema").change(function() {
$("#allchekc").prop("checked", $(".itema:checked").length == $(".itema").length)
});
});
</script>
</head>
<body>
<input type="checkbox" id="allchekc" />全选
<input type="checkbox" class="itema" />选项1
<input type="checkbox" class="itema" />选项2
<input type="checkbox" class="itema" />选项3
</body>
</html>
效果图: