2012-03-12 129 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」);