<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
      var count=document.mailfor.mail.length;
  var ch=document.mailfor;
    function allCheck(){
//    var count=document.mailfor.mail.length;
//    var ch=document.mailfor;
  for(var i=0;i<count;i++)
  {
  ch.mail[i].checked=ch.allcheck.checked;
  }
  
  }
     function allCancel(){
//       var count=document.mailfor.mail.length;
//       var ch=document.mailfor;
  for(var i=0;i<count;i++)
  {
  if(ch.mail[i].checked==true)
  ch.mail[i].checked=false;
  }
  }
     function Fc(){
  
  for(var i=0;i<count;i++)
  {
         ch.mail[i].checked=!ch.mail[i].checked;
  }
  }
</script>
</head>
<body><form name="mailfor">
<input type="checkbox" name="mail" value="1">邮箱1<br /> 
<input type="checkbox" name="mail" value="2">邮箱2<br /> 
<input type="checkbox" name="mail" value="3">邮箱3<br /> 
<input type="checkbox" name="mail" value="4">邮箱4<br /> 
<input type="checkbox" name="allcheck" onclick="allCheck()" >全选<br /> 
<input type="button" name="cancel" onclick="allCancel()" value="取消"><br /> 
<input type="button" name="fc" onclick="Fc()" value="反选"><br />
</form>
</body>
</html>我把注释的去掉就可以用,不过感觉不够简洁,设了全局变量之后,貌似用不了那些功能了!

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
        <script type="text/javascript">
            function fun(o){
                var input=document.getElementsByName("mail");
                for(var i= 0,l=input.length;i<l;i++){
                    if(o==""){
                        input[i].checked=!input[i].checked;
                    }else{
                        input[i].checked=o;
                    }
                }
            }
        </script>
    </head>
    <body>
    <form name="mailfor">
        <input type="checkbox" name="mail" value="1">邮箱1<br />
        <input type="checkbox" name="mail" value="2">邮箱2<br />
        <input type="checkbox" name="mail" value="3">邮箱3<br />
        <input type="checkbox" name="mail" value="4">邮箱4<br />
        <input type="checkbox" name="allcheck" onclick="fun(true)" >全选<br />
        <input type="button" name="cancel" onclick="fun(false)" value="取消"><br />
        <input type="button" name="fc" onclick="fun('')" value="反选"><br />
    </form>
    </body>
    </html>这样简单一点。
      

  2.   

    脚本放到 form 之后。或者 在onload 事件执行 脚本