<html>
<head>
<title>无标题文档</title>
</head>
<body>
<form name="form1" method="post" action="">
  <input type="checkbox" name="checkbox1" value="checkbox" checked="checked">
  <input type="checkbox" name="checkbox2" value="checkbox">
  <input type="checkbox" name="checkbox3" value="checkbox" checked="checked">
  <input type="checkbox" name="checkbox4" value="checkbox">
  <input type="submit" name="Submit" value="提交">
</form>
</body>
</html>
<script>
var mystr=document.form1.getElementsByTagName("input");
for(i=0;i<mystr.length;i++){
if(mystr[i].type="checkbox"){
if(mystr[i].checked==false)
alert("没选中");
else
alert("选中");
}
}
</script>

解决方案 »

  1.   


    <html>
    <head>
    <title>无标题文档</title>
    </head>
    <body>
    <form name="form1" method="post" action="">
      <input type="checkbox" id="checkbox" value="checkbox">
      <input type="checkbox" id="checkbox" value="checkbox">
      <input type="checkbox" id="checkbox" value="checkbox">
      <input type="checkbox" id="checkbox" value="checkbox">
      <input type="button" name="Submit" onclick="CheckBoxOne('checkbox','没有选择','只能选择一个')" value="提交">
    </form>
    </body>
    </html>
    <script>
    var $N = function (el) {
         return (typeof el == 'object')?el:document.getElementsByName(el) ;
    };
    ///<summary>
    ///CheckBox判断 - 只允许One选择
    ///</summary>
    ///<param name="control">控件的名称</param>
    ///<param name="mes0">没有选择一个的信息</param>
    ///<param name="mes1">选择多于一个的信息</param>
    var CheckBoxOne = function(control,mes0,mes1){
      for(var i= 0,j=0;i < $N(control).length; i++){
        if($N(control)[i].checked){    
            j++;
        }
      }if(j ==0){
        alert(mes0);
        return true;
      }if(j >1){
         alert(mes1);
         return true;
      }
      return false;
    };</script>