2012-03-12 126 views
2

这是不正常的代码:我希望它的 “Button1的”,当点击提交使用Javascript - 表单提交不工作

<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="[email protected]" > 

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

}

,使用“的javascript :donate(paypal)“提交按钮工作正常.. (它打印”可能“在formtesting div但不是”工作吗?“:/)

+1

据我所知,我不认为submit()后面的任何代码都会执行。如果我错了,请纠正我的错误 – 2012-03-12 10:09:14

+1

这是一个错字还是你的'

'标签真的在这段代码的第一行被关闭? – n1313 2012-03-12 10:11:03

回答

2

使用此代码,将工作

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是形式的名称。

+0

这与使用形式[“贝宝”]一样。 – King 2012-03-12 10:19:43

+0

您正在使用哪种浏览器? – 2012-03-12 10:22:15

+0

IE 9和Chrome – King 2012-03-12 10:22:43

-1

此作品:

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

<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="[email protected]" > 

    <a href="javascript: donate(paypal); return false" class="button1"> 
     <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> 
+0

Dosen't为我工作? – King 2012-03-12 10:17:53

+0

为什么具体是什么? – user973254 2012-03-12 10:22:37

+0

不知道这意味着什么...但上面的人固定它(这是形式[“贝宝”]位,没有调用的形式。) – King 2012-03-12 10:26:56

20

我碰到了myForm.submit()这个问题没有提交表格。原来,我的问题是我的提交按钮的id是'提交'。一旦我将我的ID改为另一个名字,中提琴,它的工作。希望这可以帮助那些遇到这个问题的相同变体的其他人。

编辑:另外请记MalcomOcean的评论,下面所组成,其中name标签也可能导致此问题

+11

谢谢。虽然在我的情况下,它是提交按钮的“名称”,而不是“id”。 – MalcolmOcean 2014-09-14 14:29:05

1

面临着同样的问题,从JavaScript提交不提交表单。试过从document.formname.submit()或document.getElementById(“formId”)。submit()提交表单的所有方法。但这些没有奏效。

问题出在我的html页面,有另一个按钮(不是我点击的那个按钮),并且该按钮的名称是“提交”。当我将该名称改为其他名称时,我的表单正在提交。

-4

请确保您使用的是getElementById而不是getElementByid

+2

有时候? JavaScript何时不区分大小写?此外,这个答案似乎与这个问题没有任何关系。 – Xufox 2016-01-11 07:47:40

0

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

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

    document.fms.submit(); 
} 

仔细检查所有引用的元素。

0

删除所有“<”窗体“>”打开和关闭标签,如果您使用document.createElement(“form”);