关于复选框全选,当其中的一个复选框去掉勾时,全选的复选框的勾也去了,当除了全选框的其他复选框都被选中时,全选框也被选中。

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>llr</title>

<script language="javascript">

  //复选框全选的方法
  function selAll(obj)
{
    var o=document.getElementsByName("che");
    for(var i=0;i<o.length;i++)
    {
        if(obj.checked==true)
            o[i].checked=true;
        else
            o[i].checked=false;
    }
}

 
  //判断是否全部被选中,如果选中全选复选框被选中,反之未被选中
   function selFirst()
   {
      var o=document.getElementsByName("che");
      var count=0,num=0;
      for(var i=0;i<o.length-1;i++)
      {
        if(o[i+1].checked==true)
        {
          count++;
        }
        if(o[i+1].checked==false)
        {
          num++;
        }
      }
      if(count==o.length-1)
      {
        o[0].checked=true;
      }
      if(num>0)
      {
        if(o[0].checked==true)
        {
          o[0].checked=false;
        }
      }
   }
  </script>
  </head>
<body leftmargin="5" topmargin="5" >
  <table width="700" border="0" id="table" cellpadding="2" cellspacing="1" class="maintable" align="center" style="margin-top:8px" >
 
<tr align="center" class="tr2" >
 <td><input name="che" type="checkbox" value="1" οnclick="selAll(this)">全选</td>
 
</tr>

  <tr class="tr2" align="center" >
  <td><input name="che" type="checkbox" οnclick="selFirst()" value="<s:property value="1"/>1</td>
  <td><input name="che" type="checkbox" οnclick="selFirst()" value="<s:property value="2"/>2</td>
   <td><input name="che" type="checkbox" οnclick="selFirst()" value="<s:property value="3"/>3</td>
   </tr>
 
 
</table>
 
</body>
</html>

猜你喜欢

转载自blog.csdn.net/lileronglilerong/article/details/8991748