表单全选取消全选

要求:点击最上方富文本框全选下方所有富文本框,点击下方所有富文本框选中最上方富文本框

效果展示


快速生成代码

1
.wrap>table>(thead>tr>(th>input:checkbox[id="selAll"])+th{商品}+th{价钱})+tbody>(tr>(td>input:checkbox)+td{iPhone8}+td{8000})+(tr>(td>input:checkbox)+td{iPad Pro}+td{5000})+(tr>(td>input:checkbox)+td{iPad Air}+td{2000})+(tr>(td>input:checkbox)+td{Apple Watch}+td{2000})

快速生成框架

1
.wrap>table>(thead>tr>(th>input:checkbox[id="selAll"])+th*2)+tbody>tr*4>(td>input:checkbox)+td*2

标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!-- index.html -->
<div class="wrap">
<table>
<thead>
<tr>
<th>
<input type="checkbox" id="selAll">
</th>
<th>商品</th>
<th>价钱</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox">
</td>
<td>iPhone8</td>
<td>8000</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>iPad Pro</td>
<td>5000</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>iPad Air</td>
<td>2000</td>
</tr>
<tr>
<td>
<input type="checkbox">
</td>
<td>Apple Watch</td>
<td>2000</td>
</tr>
</tbody>
</table>
</div>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
* {
margin: 0;
padding: 0;
box-sizing: border-box;
word-wrap: break-word;
-webkit-tap-highlight-color: transparent;
word-break: break-all;
}
table {
margin: 120px auto 0;
border-collapse: collapse;
}
input {
cursor: pointer;
}
thead tr *,
tbody tr * {
padding: 10px;
border: 1px solid #999;
}
thead {
background-color: rgb(69, 122, 214);
color: #fff;
}
thead tr th {
padding: 10px 20px;
}
thead tr th:nth-child(2) {
width: 200px;
}
tbody tr {
background-color: #f0f0f0;
}
tbody tr:hover {
cursor: pointer;
background-color: #fafafa;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var selAll = document.getElementById("selAll")
var cks = document.querySelector('tbody').querySelectorAll('input')
selAll.onclick = function() {
for(var i = 0; i < cks.length; i++) {
cks[i].checked = this.checked
}
}
for(var i = 0; i < cks.length; i++) {
cks[i].onclick = function() {
var flag = true
for(var i = 0; i < cks.length; i++) {
if(!cks[i].checked) {
flag = false
break
}
}
selAll.checked = flag
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 获取全选按钮
var selAll = document.getElementById("selAll")
// 获取单个按钮
var cks = document.querySelector('tbody').querySelectorAll('input')

selAll.onclick = function() {
cks.forEach(x => x.checked = this.checked)
}

cks.forEach(x => {
x.onclick = function() {
let flag = true
cks.forEach(y => {
if(!y.checked) {
flag = false
return
}
})
selAll.checked = flag
}
})