一、实例HTML结构
<form>
<div><input name="selectall" value="全选" type="checkbox" id="selectall" /> <label for="selectall">全选/不选</label></div>
<p><input name="checkbox" value="Item1" type="checkbox" /> 1</p>
<p><input name="checkbox" value="Item2" type="checkbox" /> 2</p>
<p><input name="checkbox" value="Item3" type="checkbox" /> 3</p>
</form>
二、JS代码
<script>
var targets=document.getElementsByName("checkbox");
var targetsLen=targets.length;
var status=0;
document.getElementById("selectall").onclick=function(){
if (status==0){
for (var i=0; i<targetsLen; i++){
targets[i].checked=true;
}
status=1;
}
else {
for (var i=0; i<targetsLen; i++){
targets[i].checked=false;
}
status=0;
}
}
</script>