<form name=meizz>
<input type=checkbox name=All onclick="checkAll('mm')">&#555;&#1121;<br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/>
<input type=checkbox name=mm onclick="checkItem('All')"><br/><br/>
<input type=checkbox name=All2 onclick="checkAll('mm2')">&#555;&#1121;<br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/>
<input type=checkbox name=mm2 onclick="checkItem('All2')"><br/></form><SCRIPT LANGUAGE="JavaScript">
function checkAll(str)
{
  var a = document.getElementsByName(str);
  var n = a.length;
  for (var i=0; i<n; i++)
  a[i].checked = window.event.srcElement.checked;
}
function checkItem(str)
{
  var e = window.event.srcElement;
  var all = eval("document.meizz."+ str);
  if (e.checked)
  {
    var a = document.getElementsByName(e.name);
    all.checked = true;
    for (var i=0; i<a.length; i++)
    {
      if (!a[i].checked){ all.checked = false; break;}
    }
  }
  else all.checked = false;
}
</SCRIPT>

解决方案 »

  1.   

    全选一的名字取名为chkone,属于它的复选框名字为chkone_child
    全选一的名字取名为chktwo,属于它的复选框名字为chktwo_child
    在它们加上事件onclick=f(this)
    <script>
    function f(obj){
     var o=obj.form.elements;
     for(var i=0;i<o.length;i++){
       if(o[i].type=='checkbox'&&o[i].name==(obj.name+'_child')){
        o[i].checked=obj.checked;
       }
     }
    }
    </script>
    利用合理的命名可以解决很多问题!
      

  2.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <body>
    <form name="form1">
    <p><input type="checkbox" name="checkbox" id="checkbox0" value="checkbox" onclick="fullcheck();"> 全选一</p>
    <p>图书一<input type="checkbox" name="checkbox" id="check0" value="checkbox"></p>
    <p>图书二 <input type="checkbox" name="checkbox" id="check0" value="checkbox"></p>
    <p>图书三 <input type="checkbox" name="checkbox" id="check0" value="checkbox"></p>
    <p>-------------</p>
    <p>
      <input type="checkbox" name="checkbox" id="checkbox1" value="checkbox"  onclick="fullcheckbox(this);">全选二</p>
    <p>磁带一 <input type="checkbox" name="checkbox" id="check1" value="checkbox"></p>
    <p>磁场二 <input type="checkbox" name="checkbox" id="check1" value="checkbox"></p>
    <p>磁带三 <input type="checkbox" name="checkbox" id="check1" value="checkbox" ></p>
    <p>&nbsp; </p>
    <input type="submit" value="submit">
    </form>
    </body>
    <script>
    function fullcheckbox(checkbox)
    {
    if(document.all.checkbox1.checked==true)
    {
    for(i=0;i<document.all.check1.length;i++)
    {
    document.all.check1[i].checked=true;
    }
    }
    else
    {
    for(i=0;i<document.all.check1.length;i++)
    {
    document.all.check1[i].checked=false;
    }
    }
    }
    function fullcheck(checkbox)
    {
    if(document.all.checkbox0.checked==true)
    {
    for(i=0;i<document.all.check0.length;i++)
    {
    document.all.check0[i].checked=true;
    }
    }
    else
    {
    for(i=0;i<document.all.check0.length;i++)
    {
    document.all.check0[i].checked=false;
    }
    }
    }
    </script>
    </html>