This article describes the method, the trigger condition is independent, you can select or deselect all the designated name checkbox, the same page can have more than one group for the checkbox, the function is sound, general strong.
Effect Chart:
To run the View effect:
<body> <script type= "Text/javascript" > Function checkall (name) {var el = document.getelementsbytagname (' I Nput '); var len = el.length; for (var i=0; i<len; i++) {if (el[i].type== "checkbox") && (El[i].name==name)) {el[i].checked = true; }} function ClearAll (name) {var el = document.getelementsbytagname (' input '); var len = el.length; for (var i=0; i<len; i++) {if (el[i].type== "checkbox") && (El[i].name==name)) {el[i].checked = false; }} </script> <input type= "checkbox" name= "Test" value= "onclick=" if (this.checked==true) {checkall (' test ') ); else {clearall (' Test ');} '/> Alphabetical switch <input type= checkbox ' name= ' Test ' value= ' a '/> A <input type= ' check Box "name=" Test "value=" B "/> b <input type=" checkbox "name=" Test "value=" C "/> C <input type=" checkbox "Nam E= "Test" value= "D"/> D <input type= "checkbox" name= "Test" value= "E"/> e <input type= "checkbox" name= "Test" Value= "F"/> F <input type= "checkbox" name= "Test" value= "G"/> g <br> <input type= "checkbox" Name= "num" value= "" Oncl Ick= ' if (this.checked==true) {checkall (' num ');} else {clearall (' num ');} "/> Digital all switches <input type=" checkbox "Name=" n Um "value=" 1 "/> 1 <input type=" checkbox "Name=" num "value=" 2 "/> 2 <input type=" checkbox "Name=" num "value= "3"/> 3 <br><br> <input type= "button" value= "Select All Letters" onclick= "Checkall (' Test ')"/> <input type = "button" value= "clears the selected letter" onclick= "ClearAll (' Test ')"/> <br><br> <input type= "button" value= " Select All digital "onclick=" checkall (' num ') "/> <input type=" button "value=" to empty the selected number "onclick=" clearall (' num ')/> </ Body> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
JS function
Copy Code code as follows:
<script type= "Text/javascript" >
function Checkall (name) {
var el = document.getelementsbytagname (' input ');
var len = el.length;
for (var i=0; i<len; i++) {
if ((el[i].type== "checkbox") && (El[i].name==name)) {
El[i].checked = true;
}
}
}
function ClearAll (name) {
var el = document.getelementsbytagname (' input ');
var len = el.length;
for (var i=0; i<len; i++) {
if ((el[i].type== "checkbox") && (El[i].name==name)) {
el[i].checked = false;
}
}
}
</script>
Html
Copy Code code as follows:
<input type= "checkbox" name= "Test" value= "onclick=" if (this.checked==true) {checkall (' Test ');} else {ClearAll (' Test '); } "/> Letter all switch
<input type= "checkbox" name= "Test" value= "a"/> A
<input type= "checkbox" name= "Test" value= "B"/> b
<input type= "checkbox" name= "Test" value= "C"/> C
<input type= "checkbox" name= "Test" value= "D"/> D
<input type= "checkbox" name= "Test" value= "E"/> E
<input type= "checkbox" name= "Test" value= "F"/> F
<input type= "checkbox" name= "Test" value= "G"/> g
<br/>
<input type= "checkbox" Name= "num" value= "onclick=" if (this.checked==true) {checkall (' num ');} else {clearall (' num ' ); } "/> Digital switch <input type=" checkbox "Name=" num "value=" 1 "/> 1
<input type= "checkbox" Name= "num" value= "2"/> 2
<input type= "checkbox" Name= "num" value= "3"/> 3
<br/>
<br/>
<input type= "button" value= "Select all the Letters" onclick= "Checkall (' Test ')" "/> <input type=" button "value=" Empty Selected letters " onclick= "ClearAll (' Test ')"/> <br/>
<br/>
<input type= "button" value= "Select all Numbers" onclick= "checkall (' num ')"/> <input "button" type= "Empty selected number" onclick= "ClearAll (' num ')"/>