js small function 1: Select All Select None

  <form>
请选择你爱好:<br> <input type="checkbox" name="hobby" id="hobby1"> 音乐 <input type="checkbox" name="hobby" id="hobby2"> 登山 <input type="checkbox" name="hobby" id="hobby3"> 游泳 <input type="checkbox" name="hobby" id="hobby4"> 阅读 <input type="checkbox" name="hobby" id="hobby5"> 打球 <input type="checkbox" name="hobby" id="hobby6"> 跑步 <br> <input type="button" value = "全选" onclick = "checkall();"> <input type="button" value = "全不选" onclick = "clearall();">
</
form>
<script>
function checkall(){
var input_all = document.getElementsByName("hobby");
for(var i=0;i<input_all.length;i++){
if(input_all[i].type==='checkbox'){
input_all[i].checked=true
}
}
}
function clearall(){
var input_all = document.getElementsByName("hobby");
for(var i=0;i<input_all.length;i++){
if(input_all[i].type==='checkbox'){
input_all[i].checked=false
}
}
}


</script>

 

Guess you like

Origin www.cnblogs.com/colorful-paopao1/p/11200443.html