abstract:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JavaScript全选</title></head><style> &nb
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript全选</title>
</head>
<style>
.box{width: 120px; margin: 100px auto;border: 1px solid; border-radius: 5px;padding:5px 0px 5px 20px;}
.box1{border-bottom:1px solid;}
input{margin: 8px}
</style>
<body>
<div class="box">
<div class="box1">
<input type="checkbox" id="checkall" onclick="checkAll()"><lable for="checkall">全选</lable>
</div>
<div class="box2">
<input type="checkbox" name ="item[]">选项1 <br/>
<input type="checkbox" name ="item[]">选项2 <br/>
<input type="checkbox" name ="item[]">选项3 <br/>
<input type="checkbox" name ="item[]">选项4 <br/>
<input type="checkbox" name ="item[]">选项5 <br/>
<input type="checkbox" name ="item[]">选项6 <br/>
</div>
</div>
<script>
function checkAll() {
var checkall=document.getElementById('checkall')
var item = document.getElementsByName("item[]")
for (var i=0;i<item.length;i++){
if(checkall.checked){
item[i].checked=true;
}else{
item[i].checked=false;
}
}
}
</script>
</body>
</html>
Correcting teacher:天蓬老师Correction time:2019-02-12 17:00:20
Teacher's summary:name ="item[]", 这样写是给后端php读取的, 他会把它识别为数组元素, 不过是索引键名,如果想自定义键名,可以使用字符串方式,例如:
name ="item['one']", name ="item['two']", 这样写也是可以的