<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(this)">
  <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>

解决方案 »

  1.   

    啊~数目错了……
    <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>2)
      {
             alert("最多只能選取2個");
             obj.checked=false;
      }
    }
    </script>
    <form name="f1" action="" method="post" onclick="check(this)">
      <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>