0

我需要能够为多个表单提供一个提交按钮。为了简单起见,我只从我的编码中取了两种形式。每个表格都有自己唯一的 ID,但每个表格都非常相似,除了一些差异。我的问题是只有第一个表单提交成功。我意识到原因是我的输入字段在每个表单中都具有相同的名称,因此它目前无法识别重复的输入字段。有没有办法即使输入字段相同也可以成功提交每个字段,我希望在帮助下我能够提交'input_1'值并将其处理为'form1:input_1'和'form2:input_1 ' 分别。非常感谢您提前

     <body><form name="form1">
     <input type="hidden" name="formID" value="form2"/>
     <input type="hidden" name="redirect_to" value=""/>
     <INPUT TYPE=TEXT NAME="input_1" SIZE=10 />
     <INPUT TYPE=TEXT NAME="input_A" SIZE=15>/<INPUT TYPE=TEXT NAME="input_C"style="width: 1em" maxlength="1"><sup>s</sup>

     <INPUT TYPE=TEXT NAME="input_B" SIZE=10 />
     <INPUT TYPE="button" VALUE="+" name="SubtractButton" onkeydown="CalculateIMSUB(this.form)">
     <INPUT TYPE=TEXT NAME="Answer" SIZE=12>
     <input type="hidden" name="val" value="298" />
     <INPUT TYPE=TEXT NAME="Answer_2" SIZE=4></form>

     <form name="form2">
     <input type="hidden" name="formID" value="form2"/>
     <input type="hidden" name="redirect_to" value=""/>
     <INPUT TYPE=TEXT NAME="input_1" SIZE=10 />
     <INPUT TYPE=TEXT NAME="input_A" SIZE=15>/<INPUT TYPE=TEXT NAME="input_C"style="width: 1em" maxlength="1"><sup>s</sup>

     <INPUT TYPE=TEXT NAME="input_B" SIZE=10 />
     <INPUT TYPE="button" VALUE="+" name="SubtractButton" onkeydown="CalculateIMSUB(this.form)">
     <INPUT TYPE=TEXT NAME="Answer" SIZE=12>
     <input type="hidden" name="val" value="298" />
     <INPUT TYPE=TEXT NAME="Answer_2" SIZE=4></form>
     <input type="submit" name="Submit" id="button" value="Submit" onClick="submitAllDocumentForms()"></body>

Javascript代码:

    <script language="javascript" type="text/javascript">
    /* Collect all forms in document to one and post it */
    function submitAllDocumentForms() {
    var arrDocForms = document.getElementsByTagName('form');
    var formCollector = document.createElement("form");
    with(formCollector)
    {
    method = "post";
    action = "process.php";
    name = "formCollector";
    id = "formCollector";
    }
    for(var ix=0;ix<arrDocForms.length;ix++) {
    appendFormVals2Form(arrDocForms[ix], formCollector);
    }
    document.body.appendChild(formCollector);
    formCollector.submit();

    }
    /* Function: add all elements from ``frmCollectFrom´´ and append them to ``frmCollector´´ before returning ``frmCollector´´*/
    function appendFormVals2Form(frmCollectFrom, frmCollector) {
    var frm = frmCollectFrom.elements; 
    var nElems = frm.length;
    for(var ix = nElems - 1; ix >= 0 ; ix--)
    frmCollector.appendChild(frm[ix]);
    return frmCollector;
    }
    </script>
4

1 回答 1

0

name 不是表单标签的标准属性。我建议您使用 id 来识别您的表单。

我尚未测试以下更改,但我希望它们能够工作:

function appendFormVals2Form(frmCollectFrom, frmCollector) {
    var currentEl;
    var frm = frmCollectFrom.elements; 
    var nElems = frm.length;
    for(var ix = nElems - 1; ix >= 0 ; ix--) {
        currentEl = frm[ix];
        currentEl.name = frmCollectFrom.name + ':' + currentEl.name;
        frmCollector.appendChild(currentEl);
    }
    return frmCollector;
}
于 2012-08-08T20:35:41.773 回答