<form id=frm>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<script>
function checknum()
{
var num;
num=0;
for (i=0;i<=document.all.frm.elements.length-1;i++)
{
   if (document.all.frm.elements[i].checked)
    {
         num+=1;
         if (num>=5)
          {
              event.returnValue=false;
          }
    }
}
}
</script>
</form>

解决方案 »

  1.   

    <input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox>
    <input onclick="aa()" type=button value=check>
    <script>
    n=0
    function aa()
    {
    l=document.getElementsByName("a")
    for(i=0;i<l.length;i++)
    {
    if(l[i].checked) n++
    }
    if(n>5) alert("多于5了")
    }
    </script>
      

  2.   

    <input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox>
    <input onclick="aa()" type=button value=check>
    <script>
    n=0
    function aa()
    {
    l=document.getElementsByName("a")
    for(i=0;i<l.length;i++)
    {
    if(l[i].checked) n++
    }
    if(n>5) alert("多5了")
    }
    </script>
      

  3.   

    <input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox><input name=a type=checkbox>
    <input onclick="aa()" type=button value=check>
    <script>
    function aa()
    {
    n=0
    l=document.getElementsByName("a")
    for(i=0;i<l.length;i++)
    {
    if(l[i].checked) n++
    }
    if(n>5) alert("多于5了")
    }
    </script>
      

  4.   

    net_lover(孟子E章) :)你写的。不是很好用啊。。
    要知道<input name=a >这个是会改变的呀。不过写也可以用。 wangyime(我有Web恐惧症) 
    写的相对实用一点。
      

  5.   

    <form name=form1>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
     <input type=checkbox>
    </form><script language=javascript>
    function document.onclick()
    {
      if(event.srcElement.type=="checkbox")
      {
        var e = document.form1.elements;
        var j = 0;
        for(var i=0; i<e.length; i++)
        {
          if(e[i].type=="checkbox" && e[i].checked) j++
          if(j > 5)
          {
             alert("你只能选择五个");
             event.srcElement.checked = false;
             break;
          }
        }
      }
    }
    </script>
      

  6.   

    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <input type=checkbox name=c>
    <script>
    document.onclick=check;
    function check(){
    var num=0;
    for (i=0;i<document.all.c.length;i++){
    if(document.all.c[i].checked){
    num++
    if (num>5){
    event.returnValue=false;
    }
    }
    }
    }
    </script>
      

  7.   

    一定要用循环吗?我的这个选了5个后也不能再选了<input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input type=checkbox>
    <input>
    <button>button</button>
    <script>
    var i=0;
    document.onclick=function(){
    if (event.srcElement.type=="checkbox" && event.srcElement.checked){
    if ((i++)>5-1) event.returnValue=false
    }
    }
    </script>
      

  8.   

    Go_Rush(阿舜)  你这段代码不够严谨.据你的代码: 我若是重复选择一个复选框, 点中.取消.点中.取消......呵呵,到最后我一个都选不中了.
      

  9.   

    fason(阿信) 你的代码比较容易接受。。谢了。。加分。
      

  10.   

    呵呵,是啊,写代码的时候写快了。
    发贴的时候其实就发现这个逻辑错误了。可惜
    这里不能修改帖子。我的代码再加一个判断,如果
    !checked  那么  --i 就可以了