定义一个计数变量,自己想想,不行我再给你代码

解决方案 »

  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>8)
      {
             alert("最多只能選取8個");
             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>