`

js全选/全不选

 
阅读更多
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>全选/全不选</title>
<script type="text/javascript">
//选择或取消所有选课
function selectAll(){

    if(myForm.mySelect.checked){

//通过循环将复选框组中的所有选项的checked属性值设为true

for(i=0;i<myForm.course.length;i++){


  myForm.course[i].checked=true;
}


}else{

//通过循环将复选框组中的所有选项的checked属性值设为false
for(i=0;i<myForm.course.length;i++){


  myForm.course[i].checked=false;

}

}

}

//判断“选课”复选框中的所有选项是否全选
   function  checkDate(){
  
   var checkedCount=0;
  
  
   for(i=0;i<myForm.course.length;i++){
  
  //如果选项已选中,在数量上加1
   if(myForm.course[i].checked){
  
       checkedCount++;
  
       }
  
   }
      //如果“选课”复选框组中的选项全选,则将“全选”复选框设为选中状态
   if(checkedCount==myForm.course.length){
  
          myForm.mySelect.checked=true;
 
   }else{
 
          myForm.mySelect.checked=false;
   }
  
}

</script>
</head>

<body>
<form name="myForm" method="post" id="myForm">
请选择课程:<br/>
<input  type="checkbox" name="course" value="yuwen" onclick="checkDate()" />语文<br/>

<input  type="checkbox" name="course" value="shuxue" onclick="checkDate()" />数学<br/>

<input  type="checkbox" name="course" value="english" onclick="checkDate()" />英语<br/>

<input  type="checkbox" name="course" value="wuli" onclick="checkDate()" />物理<br/>

<input  type="checkbox" name="course" value="huaxue" onclick="checkDate()" />化学<br/>

<input  type="checkbox" name="course" value="zhengzhi" onclick="checkDate()" />政治<br/>
<hr>
<input type="checkbox" name="mySelect" onclick="selectAll()" />全选/全不选<br />

<input type="submit" value="提交" />

<input type="reset" value="重置" />




</form>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics