JavaScript--点击按钮实现复选框全选和反选功能功能实现:
1、点击全选和反选按钮,实现复选框全选和部分选的功能
2、点击全选复选框,实现⼦复选框全选或全不选功能
完整代码:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
</style>
</head>
<body>
<table border="1">
<tr>
<th><input type="checkbox" class="choose-all-input" οnclick="clickChooseAllInput()" /></th>
<th>学号</th>
<th>姓名</th>
<th>班级</th>
</tr>
<tr>
<td><input type="checkbox" class="choose-single" /></td>
<td>001</td>
<td>张三</td>
<td>李四</td>
</tr>
<tr>
<td><input type="checkbox" class="choose-single" /></td>
<td>002</td>
<td>F20</td>
<td>F20</td>
</tr>
</table>
<button οnclick="clickChooseAllBtn()">全选</button>
<button οnclick="clickChooseReverse()">反选</button>
</body>
<script type="text/javascript">如何启用javascript功能
var chooseAllInputEle = ElementsByClassName("choose-all-input")[0];
var chooseSingleEles = ElementsByClassName("choose-single");
function clickChooseAllInput() {
if (chooseAllInputEle.checked) {
choose("selected");
} else {
choose("");
}
}
function clickChooseAllBtn() {
chooseAllInputEle.checked = "checked";
choose("selected");
}
function choose(status) {
for (var i = 0; i < chooseSingleEles.length; i++) {
chooseSingleEles[i].checked = status;
}
}
function clickChooseReverse() {
for (var i = 0; i < chooseSingleEles.length; i++) {
for (var i = 0; i < chooseSingleEles.length; i++) {            if (chooseSingleEles[i].checked) {
chooseSingleEles[i].checked = "";
} else {
chooseSingleEles[i].checked = "checked";            }
}
}
</script>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。