效果图如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Web Project</title>
<script type="text/javascript">
/**
* 全选
*/
function selectAll() {
var all = document.getElementById("all");
var languages = document.getElementsByName("language");
for (var i=0; i < languages.length; i++) {
languages[i].checked = all.checked;
}
}
/**
* 当下面所有的选项都被选中时,全选被选中
* 当下面所有的选项有一个没有被选中时,全选不被选中
*/
function sel() {
var all = document.getElementById("all");
var languages = document.getElementsByName("language");
var count = 0;
for (var i=0; i < languages.length; i++) {
if(languages[i].checked) {
count++;
}
}
if(count == languages.length) {
all.checked = true;
}else {
all.checked = false;
}
}
</script>
</head>
<body>
<input type="checkbox" id="all" οnclick="selectAll()" />全选<br/>
编程语言:<br/>
<input type="checkbox" name="language" value="C" οnclick="sel()" />C<br/>
<input type="checkbox" name="language" value="C++" οnclick="sel()" />C++<br/>
<input type="checkbox" name="language" value="Java" οnclick="sel()" />Java<br/>
<input type="checkbox" name="language" value="Python" οnclick="sel()" />Python<br/>
</body>
</html>