Javascript - 表单提交无效

Mr.*_*ing 2 javascript forms

这是不起作用的代码:

<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></span>
<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?";
Run Code Online (Sandbox Code Playgroud)

}

我希望它在点击"button1"时提交,使用"javascript:donate(paypal)"提交按钮工作正常..(它在formtesting div中打印"maybe"但不是"它有效吗?":/)

cos*_*str 28

我遇到的问题myForm.submit()是没有提交表格.事实证明我的问题是我的提交按钮id是'提交'.一旦我将我的身份改为另一个名字,中提琴,它就有效了.希望这有助于那些遇到此问题的同一变体的其他人.

编辑:还要记下下面MalcomOcean的评论,name标签也可能导致此问题

  • 谢谢.虽然在我的例子中它是提交按钮的`name`,而不是`id`. (13认同)

Gau*_*wal 5

使用此代码它将起作用

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?";
}
Run Code Online (Sandbox Code Playgroud)

这里的贝宝是表格的名称。