多个复选框无法从JavaScript函数访问

时间:2011-10-08 07:45:15

标签: javascript

我有动态多个复选框,用于恢复多个文件。当我有超过1个复选框时它完美地工作。这是我的复选框的PHP代码:

<form name="RestoreFile">
    <input type="checkbox" title="'.$FldDoc['FldDocumentName'].'" name="restore_checkbox" value="'.$FldDoc['FldDocumentID'].'" id="restore_'.$NodeId.'_'.$FldDoc['FldDocumentID'].'"/>
    <input type="button" value="Restore" onclick="RestoreDocFile(\''.$NodeId.'\',this.form.restore_checkbox);" />
</form>

函数RestoreDocFile()的定义如下:

function getSelected(opt) 
{
    var selected = new Array();
    var index = 0;
    for (var intLoop = 0; intLoop < opt.length; intLoop++) {
       if (opt[intLoop].checked) 
       {
          index = selected.length;
          selected[index] = new Object;
          selected[index].value = opt[intLoop].value;
          selected[index].index = intLoop;
       }
    }
    return selected;
}

function RestoreDocFile(nodeid, opt)
{   
    var getSelectDocIds = getSelected(opt);
//alert(nodeid+','+getSelectDocIds);    
        var strSelectedDocIds = "";
        var i=0;
        for (var item in getSelectDocIds)
        {
           if(i!=0)
           {
                strSelectedDocIds+=":";
           }    
           strSelectedDocIds += getSelectDocIds[item].value ;
           i++;
        }
}

问题是,如果在表单加载时有1个复选框,则它无法正常工作。

1 个答案:

答案 0 :(得分:0)

尝试替换

onclick="RestoreDocFile(\''.$NodeId.'\',this.form.restore_checkbox);"

onclick="RestoreDocFile(\''.$NodeId.'\',this.form.getElementsByName(\'restore_checkbox\'));"

无论有多少个复选框,这都将确保您获得NodeList。