递归选中某页面的checkbox函数
脚本如下:为什么只能递归到第一个分支?
比如:
1选项一
2  选项一一
3    选项一一一
4    选项一一二
5    选项一一三
6  选项一二
7    选项一二一
8    选项一二二
9  选项一三为什么下面的脚本只能将1到5的选项选上??函数说明:
obj为某父节点。
act指checkbox的name中需包含的字符串
parent和haschild为checkbox中自定义的属性
function selectchild(obj,act)
{
   var all=document.getElementsByTagName("input");
   for(i=0; i<all.length; i++)
   {
     if(all[i].type=="checkbox")
    {
      if(all[i].name.indexOf(act)!=-1)
      {
        if(all[i].parent==obj.value)
        {
 if(!all[i].disabled)
 {
            if(obj.checked)
            {
              all[i].checked=true;
   }
   else
   {
              all[i].checked=false;
   }
   if(all[i].haschild=="true")
   {
      target=obj.id;
      alert(target);
      selectchild(all[i],act);
      obj=document.getElementById(target);
      alert(target);
             }
           }
         }
       }
     }
   }
}猜测可能是引用传递的问题,可是不知道怎么解决。

解决方案 »

  1.   

    很可能是all[i].name.indexOf(act)!=-1这句话引起的
    注意包含
      

  2.   

    贴上全部最终HTML代码,可调试运行的。各位大虾们帮帮忙吧.<HTML>
    <HEAD>
    <script>
    var target;
    function selectchild(obj,act)
    {
    var all=document.getElementsByTagName("input");

    for(i=0; i<all.length; i++)
    {
    if(all[i].type=="checkbox")
    {
    if(all[i].name.indexOf(act)!=-1)
    {
    if(all[i].parent==obj.value)
    {
    if(!all[i].disabled)
    {
    if(obj.checked)
    {
    all[i].checked=true;
    }
    else
    {
    all[i].checked=false;
    }
    if(all[i].haschild=="true")
    {
    target=obj.id;
    alert(target);
    selectchild(all[i],act);
    obj=document.getElementById(target);
    alert(target);
    }
    else
    {
    continue;
    }
    }
    }
    }
    }
    }

    }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <div id="AccessGroup" style="OVERFLOW: scroll; WIDTH: 400px; POSITION: relative; HEIGHT: 200px">
    <input type="checkbox" parent="-1" id="access" name="access" value="0" onclick="selectchild(this,'access')">全部系统用户组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_1" parent="0" haschild="true" name="access_1" value="1" onclick="selectchild(this,'access')">超级管理员组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_2" parent="1" haschild="false" name="access_2" value="5">管理员一组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_3" parent="1" haschild="false" name="access_3" value="6">管理员二组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_4" parent="1" haschild="false" name="access_4" value="7">管理员三组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_5" parent="0" haschild="true" name="access_5" value="2" onclick="selectchild(this,'access')">普通用户组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_6" parent="2" haschild="false" name="access_6" value="8">普通用户一组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="checkbox" id="access_7" parent="2" haschild="false" name="access_7" value="9">普通用户二组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="access_8" parent="0" haschild="false" name="access_8" value="3">匿名用户组
    <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="access_9" parent="0" haschild="false" name="access_9" value="4">待审用户组
    </div>
    </body>
    </html>