首页 > 代码库 > JavaScript 实现的checkbox经典实例分享

JavaScript 实现的checkbox经典实例分享

JavaScript 实现的checkbox经典实例分享

本文主要给大家分享的是JavaScript实现checkbox多项选择的经典代码,非常的简单实用,有需要的小伙伴可以参考下
 

JavaScript 实现的checkbox经典实例分享

?
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>邮件删除</title>
 <style type="text/css">
  *{
  margin: 0;
  padding: 0;
  }
  table{
  width: 400px;
  margin-left:200px;
  margin-top:20px;
  font-weight: bold;
  }
  th,td{
  padding: 7px;
  }
  #topOne{
  width: 80px;
  padding: 10px;
  }
  #topTwo{
  width: 120px;
  text-align: center;
  }
  #topThree{
  width: 200px;
  text-align: center;
  }
  #endColspan{
  text-align: center;
  }
  #endColspan input{
  margin: 0 5px;
  }
 </style>
 </head>
 <body>
 <table border="2" cellspacing="0" cellpadding="">
  <!--第一行-->
  <tr id="top">
  <td id="topOne">
   <input type="checkbox" id="allInpTop" value=http://www.mamicode.com/"" />
   <span id="allSpanTop">全选</span>
  </td>
  <td id="topTwo">选择路线</td>
  <td id="topThree">请选择英雄</td>
  </tr>
   
  <!--第二行-->
  <tr id="content1">
  <td id="contentOne1">
   <input type="checkbox" name="" id="contentOne_input1" value=http://www.mamicode.com/"" />
  </td>
  <td id="contentTwo1">上单</td>
  <td id="contentThree1">德玛</td>
  </tr>
  <tr id="content2">
  <td id="contentOne2">
   <input type="checkbox" name="" id="contentOne_input2" value=http://www.mamicode.com/"" />
  </td>
  <td id="contentTwo2">中单</td>
  <td id="contentThree2">安妮</td>
  </tr>
  <tr id="content3">
  <td id="contentOne3">
   <input type="checkbox" name="" id="contentOne_input3" value=http://www.mamicode.com/"" />
  </td>
  <td id="contentTwo3">打野</td>
  <td id="contentThree3">螳螂</td>
  </tr>
   
  <!--第三行-->
  <tr id="end">
  <td id="endOne">
   <input type="checkbox" id="endOne_input" value=http://www.mamicode.com/"" />
   <span id="endOne_span">全选</span>
  </td>
  <td id="endColspan" colspan="2">
   <input type="button" name="" id="endColspan_btn1" value=http://www.mamicode.com/"全选" />
   <input type="button" name="" id="endColspan_btn2" value=http://www.mamicode.com/"取消全选" />
   <input type="button" name="" id="endColspan_btn3" value=http://www.mamicode.com/"反选" />
   <input type="button" name="" id="endColspan_btn4" value=http://www.mamicode.com/"删除所选邮件" />
  </td>
  </tr>
 </table>
 </body>
 <script type="text/javascript">
  
 var allInpTopObj = document.getElementById("allInpTop");
  
 var endOne_input = document.getElementById("endOne_input");
  
 var content1Obj = document.getElementById("content1");
 var content2Obj = document.getElementById("content2");
 var content3Obj = document.getElementById("content3");
  
 var contentOne_inputObj1 = document.getElementById("contentOne_input1");
 var contentOne_inputObj2 = document.getElementById("contentOne_input2");
 var contentOne_inputObj3 = document.getElementById("contentOne_input3");
  
 var endColspan_btn1Obj = document.getElementById("endColspan_btn1");
  
 var endColspan_btn2Obj = document.getElementById("endColspan_btn2");
  
 var endColspan_btn3Obj = document.getElementById("endColspan_btn3");
  
 var endColspan_btn4Obj = document.getElementById("endColspan_btn4");
  
 var luxianObj = document.getElementById("luxian");
  
 var renwuObj = document.getElementById("renwu");
  
  
 var strInput = [contentOne_inputObj1,contentOne_inputObj2,contentOne_inputObj3,allInpTopObj,endOne_input];
  
 var checks = [contentOne_inputObj1,contentOne_inputObj2,contentOne_inputObj3];
  
 var s = [content1Obj,content2Obj,content3Obj];
  
  
 var bool = true;
 var num;
  
  
 function pandaunFun () {
  if (allInpTopObj.checked = true) {
  allInpTopObj.checked = false;
  }
  if (endOne_input.checked = true) {
  endOne_input.checked = false;
  }
 }
  
 function allFun () {
  for (var i = 0; i < strInput.length; i++) {
  strInput[i].checked = true;//选中
  }
 }
  
 function cancelFun () {
  for (var i = 0; i < strInput.length; i++) {
  strInput[i].checked = false;//选中
  }
 }
  
 function unAllFun () {
  for (var i = 0; i < strInput.length; i++) {
  if (strInput[i].checked == true) {
   strInput[i].checked = false;//非选中
  } else{
   strInput[i].checked = true;//选中
  }
  }
   
  pandaunFun ();
 }
  
 function deleteFun () {
  for (var i = 0; i < s.length; i++) {
  if (checks[i].checked) {
   s[i].style.display = "none";
  }
  }
   
  pandaunFun ();
   
 }
  
 function numCheckFun () {
  num = 0;
  for (var j = 0; j < checks.length; j++) {
    
   if (checks[j].checked) {
   num++;
   }
  }
   
  if (num == checks.length) {
   allInpTopObj.checked = true;
   endOne_input.checked = true;
  }else{
   allInpTopObj.checked = false;
   endOne_input.checked = false;
  }
  }
  
 
 endColspan_btn1Obj.onclick = allFun;
 
 endColspan_btn2Obj.onclick = cancelFun;
 
 endColspan_btn3Obj.onclick = unAllFun;
 
 endColspan_btn4Obj.onclick = deleteFun;
 
 for (var i = 0; i < checks.length; i++) {
  checks[i].onclick = numCheckFun;
 }
 </script>
</html>

 

(转载) http://www.jb51.net/article/94864.htm

JavaScript 实现的checkbox经典实例分享