Javascript submit()没有't触发表单提交

Javascript submit() doesn't trigger form submission

本文关键字:表单提交 submit 没有 Javascript      更新时间:2023-09-26

这是不起作用的代码:

<form id="paypal" name="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent" />Official website!
    <input type="hidden" name="item_name" value="Donation">
    <input type="hidden" name="cmd" value="_donations">
    <input type="hidden" name="bn" value="PP-DonationsBF">
    <input type="hidden" name="currency_code" id="currency_code" value="GBP" >
    <input type="hidden" name="business" id="business" value="paypalemail@null.com" >
    <a href="javascript: donate(paypal);" class="button1"><span></span><strong>£<input name="amount" type="text" id="ppAmount" style="text-align:center;" value="5" size="2" /> Donate!</strong></a>
    <input type="submit">
</form>
<div id="formtesting"></div>
<script type="text/javascript">
function donate(paypal) {
    document.getElementById("formtesting").innerHTML="maybe...";
    document.forms["paypal"].action = "https://www.paypal.com/cgi-bin/webscr";
    document.forms["paypal"].submit();
    document.getElementById("formtesting").innerHTML="did it work?";
}
</script>

我希望它在点击"button1"时提交,使用"javascript:donate(paypal)"提交按钮工作正常。。(它在表单测试div中打印"maybe",但没有打印"did it work?":/)

我遇到了myForm.submit()没有提交表单的问题。原来我的问题是我的提交按钮的id是"提交"。一旦我把我的id改成另一个名字,viola,它就起作用了。希望这能帮助其他遇到同样问题的人。

编辑:也要注意MalcomOcean的评论,在下面,name标签也会导致这个问题

使用此代码,它将工作

function donate() {
document.getElementById("formtesting").innerHTML="maybe...";
document.paypal.action = "https://www.paypal.com/cgi-bin/webscr";
document.paypal.submit();
document.getElementById("formtesting").innerHTML="did it work?";
}

这里的paypal是表单的名称。

面对同样的问题,从javascript提交表单没有提交。尝试了所有从javascript提交表格的方法,如document.formname.submit()或document.getElementById("formId").submit)。但这些都不起作用。

问题出在我的html页面上,有另一个按钮(不是我点击的那个),那个按钮的名称是"提交"。当我把这个名字改成其他名字时,我的表格正在提交中。

对我来说,这是由于试图访问不存在的元素造成的。即使在开发人员控制台中查看,也不会有错误日志通知,但代码会中止。我的代码:

function MoveImageUp(id)
{
    document.fms.aid.value= id;
    document.fms.mode.value='up'; // die! the form has no element called aid
    document.fms.submit();
}

双击所有引用的图元。

删除所有"<"form">"打开和关闭标记,如果您使用的是document.createElement("form");