Using document.forms[ "MbrForm" ].submit();不起作用



我有一个表单,我需要有两种提交表单的方法。第一种方法是使用标准提交按钮。这工作得很好。seond 方法应该首先打印页面,然后显示警报,最后提交表单。第二种方式是我需要帮助的地方。

对于第二种方式,我在窗体中有一个调用"printpage"函数的按钮。此函数打印页面,显示警报,然后提交表单。除了提交表单外,它会执行所有操作。任何帮助找出原因的帮助都是值得赞赏的。

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">

为简洁起见,表单本身的内容已被删除。仅保留"提交"和"打印"按钮

            <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" />

这是一个问题。删除名称="提交",它应该可以工作。具有 name 属性的每个表单元素也会在表单对象中创建一个属性。此元素创建一个属性"submit",该属性覆盖窗体的提交方法。

我终于找到了这个问题的答案,部分归功于之前的几条评论。

解决方案是使两个按钮"提交"类型的按钮具有相同的名称。

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

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

然后在表单处理代码中检查"btntype"的值,如果值="提交",则执行A if value = "Print",如果值="打印",则执行B。"打印"提交按钮的 onclick 事件调用 printpage 函数,并在表单正常提交之前执行其所有操作。

最新更新