<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="JavaScript">
function checkboxCount(name)
{
var count = 0;
var objs = document.getElementsByName(name);
for(var i = 0; i < objs.length; i++)
{
if(objs[i].tagName == 'INPUT' && objs[i].type == 'checkbox')
if(objs[i].checked) count ++;
} return count;
}function check()
{
var count = checkboxCount('MyCheckBox');
if(count < 1)
{
alert("要选东西呀!");
}
else if(count > 10)
{
alert("太贪了吧!");
}
else
{
alert("通过啦!");
}
}
</script>
</HEAD><BODY>
<input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><br>
<input type="button" value="检查" onclick="check();">
</BODY>
</HTML>

解决方案 »

  1.   

    我总结几位红星的:
    方法一:
    <script>
    function check(obj)
    {
      var j=0;
      var el = f1.elements;
      var count = el.length;
      for (i=0;i<count;i++)
      {
    if (el[i].type=="checkbox" && el[i].checked==true)
    j++;
      }
      if(j>5)
      {
             alert("最多只能選取5個");
             obj.checked=false;
      }
    }
    </script>
    <form name="f1" action="" method="post">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
    </form>
      

  2.   

    方法二:
    <script>
    function check(obj)
    {
      var j=0;
      var el = f1.elements;
      var count = el.length;
      var obj=event.srcElement;
      for (i=0;i<count;i++)
      {
    if (el[i].type=="checkbox" && el[i].checked==true)
    j++;
      }
      if(j>5)
      {
             alert("最多只能選取5個");
             obj.checked=false;
      }
    }
    </script>
    <form name="f1" action="" method="post" onclick="check()">
      <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" >
    </form>
      

  3.   

    方法三:
    <SCRIPT LANGUAGE="JavaScript">
    var c=0,limit=5;
    function limitCheck(obj) {
    obj.checked?c++:c--;
    if(c>limit)obj.checked=false,c--;
    }
    </SCRIPT>
    <form>
    <script>
    for (var i=0;i<20;i++) {
    document.write("<input type='checkbox' name='xx' onclick='limitCheck(this)'>")
    }
    </script>
    </form>
      

  4.   

    <script>
      var j=0
    function check(obj)
    { obj.checked==true
             {
    j=j+1;
              }
              else
              {
                       j=j-1;
               }
      }
      if(j>5)
      {
             alert("最多只能選取5個");
             obj.checked=false;
      }
    }
    </script>
    <form name="f1" action="" method="post">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
      <input type="checkbox" onclick="check(this)">
    </form>
      

  5.   

    终于有人回帖了, 不让我连续三次唉,
    方法四:
    <script>
    var checked=new Array()
    function check()
      {var i,j=0;
       var el = f1.elements;
       var count = el.length;
       var obj=event.srcElement;
       checked[checked.length]=obj;
       //alert(checked[checked.length])
       for (i=0;i<count;i++)
         if (el[i].type=="checkbox" && el[i].checked==true)
           j++;
       if(j>5)
         {alert("最多只能選取5個");
          checked[checked.length-1].checked=false;
         }
      }
    </script>
    <form name="f1" action="" method="post" onclick="check()">
      <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" >
    </form>
    这几个程序不仅限制数量, 还让你最后选定的也不能选.
      

  6.   

    //函数名称:readcheckbox
    //函数功能:判断当前CheckBox被选中的个数
    //函数参数:itemname,表单项的名字
    //函数返回:返回被选中的个数
    function readcheckboxcount(itemname) {
        var thecheckboxcount;
        thecheckboxcount=0;
        var bootobject=document.all.tags("INPUT");
        for (i=0;i<bootobject.length;i++) {
    if (bootobject[i].name==itemname && bootobject[i].checked) {
    thecheckboxcount+=1;
    }
        }
    return(thecheckboxcount);
    }
      

  7.   

    通过检验 "document.all.表单名.选项.checked" 来计算吧
      

  8.   

    昨天晚上刚回答过一个类似的http://expert.csdn.net/Expert/topic/2554/2554005.xml?temp=.5121118