javascript表单复选框全选代码
时间: 2023-03-28 17:01:41 浏览: 132
以下是javascript表单复选框全选代码:
HTML代码:
```
<input type="checkbox" id="checkAll">全选
<input type="checkbox" class="checkItem">选项1
<input type="checkbox" class="checkItem">选项2
<input type="checkbox" class="checkItem">选项3
```
JavaScript代码:
```
var checkAll = document.getElementById("checkAll");
var checkItems = document.getElementsByClassName("checkItem");
checkAll.onclick = function() {
for (var i = ; i < checkItems.length; i++) {
checkItems[i].checked = checkAll.checked;
}
};
for (var i = ; i < checkItems.length; i++) {
checkItems[i].onclick = function() {
var checkedCount = ;
for (var j = ; j < checkItems.length; j++) {
if (checkItems[j].checked) {
checkedCount++;
}
}
checkAll.checked = checkedCount == checkItems.length;
};
}
```
阅读全文