<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul style="list-style: none;">
<span>楼层</span>
<li><input type="checkbox" name="hb" />一号楼1楼</li>
<li><input type="checkbox" name="hb" />一号楼2楼</li>
<li><input type="checkbox" name="hb" />一号楼3楼</li>
<li><input type="checkbox" name="hb" />一号楼4楼</li>
<li><input type="checkbox" name="hb" />一号楼5楼</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>
<script>
// 全选
function setAll() {
var hb = document.getElementsByName("hb");
for (var i = 0; i < hb.length; i++) {
hb[i].checked = true;
}
}
// 全不选
function unsetAll() {
var hb = document.getElementsByName("hb");
for (var i = 0; i < hb.length; i++) {
hb[i].checked = false;
}
}
// 反选
function setOthers() {
var hb = document.getElementsByName("hb");
for (var i = 0; i < hb.length; i++) {
if (hb[i].checked == false)
hb[i].checked = true;
else
hb[i].checked = false;
}
}
</script>
</html>
JavaScript 实现全选、全不选和反选功能(最简单)
于 2023-08-31 21:11:30 首次发布