<form name="form1">
<input id="ch" name="ch" type="checkbox" value=1>
<input id="ch" name="ch" type="checkbox" value=2>
<input id="ch" name="ch" type="checkbox" value=3>
<input type="button" onClick="alert(check())">
</form><script>
function check() {
var a=document.getElementsByName("ch");
var b=[];
for(var i=0; i<a.length; i++) {
if(a[i].checked) {
b.push(a[i].value);
}
}
return b;
}
</script>

解决方案 »

  1.   

    <form name="form1">
    <input name="id" type="checkbox" value=1>
    <input name="id" type="checkbox" value=2>
    <input name="id" type="checkbox" value=3>
    <input type="button" onClick="check()">
    </form>
    <script>
      function check(){
        objs = document.getElementsByName('id');
    for(i=0; i<objs.length; i++) {
       if(objs[i].checked == true){
          alert('checkbox'+i+' = '+objs[i].value);     
       } 
    }
      }
    </script>
      

  2.   

    <form name="form1">
    <input id="ch" name="ch" type="checkbox" value=1>
    <input id="ch" name="ch" type="checkbox" value=2>
    <input id="ch" name="ch" type="checkbox" value=3>
    <input type="button" onClick="alert(check())">
    </form><script>
    function check() {
    var a=document.getElementsByName("ch");
    var b=[];
    for(var i=0; i<a.length; i++) {
    if(a[i].checked) {
    b.push(a[i].value);
    }
    }
    if(b.length>1) {
    alert('only one')
    return false;
    }
    return b;
    }
    </script>