代码
#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全选</title>
</head>
<body>
<ul style="list-style: none;">
<span>选择爱好</span>
<li><input type="checkbox" name="hb" />爬山</li>
<li><input type="checkbox" name="hb" />游泳</li>
<li><input type="checkbox" name="hb" />攀岩</li>
<li><input type="checkbox" name="hb" />冲浪</li>
<li><input type="checkbox" name="hb" />说唱</li>
</ul>
<input type="button" onclick="setAll()" value="全选" id="qx" />
<input type="button" onclick="unsetAll()" value="全不选" id="qbx" />
<input type="button" onclick="setOthers()" value="反选" id="fx" />
</body>
</html>
--------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全选</title>
</head>
<body>
<ul style="list-style: none;">
<span>选择爱好</span>
<li><input type="checkbox" name="hb" />爬山</li>
<li><input type="checkbox" name="hb" />游泳</li>
<li><input type="checkbox" name="hb" />攀岩</li>
<li><input type="checkbox" name="hb" />冲浪</li>
<li><input type="checkbox" name="hb" />说唱</li>
</ul>
<input type="button" onclick="setAll()" value="全选" id="qx" />
<input type="button" onclick="unsetAll()" value="全不选" id="qbx" />
<input type="button" onclick="setOthers()" value="反选" id="fx" />
<script>
var inp = document.getElementsByName('hb')
function setAll() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = true
}
}
function unsetAll() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = false
}
}
function setOthers() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = !inp[i].checked
}
}
</script>
</body>
</html>
验证
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全选</title>
</head>
<body>
<ul style="list-style: none;">
<span>选择爱好</span>
<li><input type="checkbox" name="hb" />爬山</li>
<li><input type="checkbox" name="hb" />游泳</li>
<li><input type="checkbox" name="hb" />攀岩</li>
<li><input type="checkbox" name="hb" />冲浪</li>
<li><input type="checkbox" name="hb" />说唱</li>
</ul>
<input type="button" onclick="setAll()" value="全选" id="qx" />
<input type="button" onclick="unsetAll()" value="全不选" id="qbx" />
<input type="button" onclick="setOthers()" value="反选" id="fx" />
<script>
var inp = document.getElementsByName('hb')
function setAll() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = true
}
}
function unsetAll() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = false
}
}
function setOthers() {
for (var i = 0; i < inp.length; i++) {
inp[i].checked = !inp[i].checked
}
}
</script>
</body>
</html>