简体   繁体   English

using document.forms [“MbrForm”]。submit(); 不起作用

[英]using document.forms[“MbrForm”].submit(); does not work

I have a form that I need to have two means of submitting the form. 我有一个表格,我需要有两种方式提交表格。 The first way is with the standard submit button. 第一种方法是使用标准提交按钮。 This works just fine. 这很好用。 The seond way is supposed to first print the page, then display an alert and finally submit the form. 应该首先打印页面,然后显示警报并最终提交表单。 This second way is where I need the help. 第二种方式是我需要帮助的地方。

For the second way I have a button in the form that calls the "printpage" function. 对于第二种方式,我在窗体中有一个调用“printpage”函数的按钮。 This function prints the page, displays an alert and then is supposed to submit the form. 此功能打印页面,显示警报,然后应该提交表单。 It does everything except for submit the form. 它提供除提交表格之外的所有内容。 Any help figuring out why is appreciated. 任何帮助搞清楚为什么值得赞赏。

The code for the htm page is shown below. htm页面的代码如下所示。

<body>
<script type="text/javascript"> 

function printpage()
{
    if (MbrForm_Validator(document.forms["MbrForm"]))
    {
    window.print();
    alert("Thank you for your support of the Friends. Please mail your form and check to the address shown on the printed        page.");
//***********************************************************************************
//***********************************************************************************
//this is the line of code that does not work for me        
        document.forms["MbrForm"].submit();
//***********************************************************************************
//***********************************************************************************
    }
}

function MbrForm_Validator(theform)
{
//removed for brevity
}
</script>

        <h1>Membership Application</h1>
        <p style="margin:0px;text-size:9px;"><sup class="required">*</sup>Denotes required information</p>
        <form id="MbrForm" name="MbrForm" method="post" action="membertest.php" onsubmit="return MbrForm_Validator(this)" target="_blank">

the contents of the form itself have been removed for brevity. 为简洁起见,已删除表单本身的内容。 Only the SUBMIT and PRINT buttons remain 仅保留SUBMIT和PRINT按钮

            <table width="75%" border="0" align="center" cellspacing="5" class="displayonly">
                <caption style="border-bottom:black solid 1px;">
                    How Would You Like to Pay? 
                </caption>
                <tr>
                    <td width="49%" valign="top">
                        With a check<br />
                        Click the "Print" button below to print this form for mailing in with your check.
                    </td>
                    <td width="51%" valign="top">
                        On-Line with a Credit Card<br />
                        Click the "Submit" button below to submit your information and pay using a credit card or PayPal.
                    </td>
                </tr>
                <tr>
                    <td>
                        <div align="left">
                            <input type="button" name="btnprint" id="btnprint" value="Print" onclick="printpage()" />
                        </div>
                    </td>
                    <td>
                        <div align="left">
                            <input type="submit" name="submit" id="submit" value="Submit" />
                        </div>
                    </td>
                </tr>
            </table>

        </form>
        <div class="printonly">
            <p>
                <br /><br /><br /><br /><br />
                <hr />
                Mail with your check to:<br />
            </p>
        </div>
    </div>


</div>
</body>
<input type="submit" name="submit" id="submit" value="Submit" />

This is a problem. 这是个问题。 Remove the name="submit" and it should work. 删除name =“submit”,它应该工作。 Each of the form elements that have the name attribute also create a property in the form object. 每个具有name属性的表单元素也在表单对象中创建一个属性。 This element creates a property "submit" which overwrites the form's submit method. 此元素创建一个属性“submit”,它覆盖表单的submit方法。

I finally found the answer to this problem, thanks in part to the several previous comments. 我终于找到了这个问题的答案,部分归功于之前的几条评论。

The solution is to make both buttons "Submit" type buttons with the same name. 解决方案是使两个按钮“提交”类型按钮具有相同的名称。

<input type="submit" name="submit" id="submit" value="Submit" />
<input type="button" name="btnprint" id="btnprint" value="Print" onclick="printpage()" />

to

<input type="submit" name="btntype" value="Submit" />
<input type="submit" name="btntype" value="Print" onclick="printpage()" />

Then in the form processing code check the value of "btntype" and do A if value = "Submit" and B if value = "Print". 然后在表单处理代码中检查“btntype”的值,如果value =“Submit”则执行A,如果value =“Print”则执行B. The onclick event for the "Print" submit button calls the printpage function and does all of its stuff before the form is submitted normally. “打印”提交按钮的onclick事件调用printpage函数,并在表单正常提交之前完成所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM