2012-01-19 49 views
0

我有我的網站不能正常工作的代碼,我一直無法弄清楚爲什麼...JavaScript表單提交失敗

下面是代碼:

if (self.location.href == top.location.href) { 
    document.fastform.submit(); 
document.getElementById(fastform).submit(); 
} 

現在如果我把表單以外的其他東西提交到if語句中,它可以正常工作。這只是當我做表單提交它永遠不會奏效代碼...

這裏是表單代碼:

<form id="fastform" name="fastform" ACTION="/amember.php"> 
<INPUT TYPE="text" NAME="myurl" ID="myurl"> 
<input type="submit" /> 
</form> 

感謝您的幫助傢伙!

到目前爲止,沒有任何建議的工作,我嘗試了幾種不同的變化,像在getelementbyid周圍的fastform引號。這是我的整個javascript程序:

<script type="text/javascript"> 

function geturl() { 
var locate = document.location 
document.fastform.myurl.value = locate 
} 
window.onload = geturl; 

if (self.location.href == top.location.href) { 
var f=document.forms.fastform; f.submit(); 
} 

</script> 

感謝您的建議!

好的,所以在這裏使用一些建議的代碼我得到了它的工作。問題在於if語句沒有在正確的時間執行,我移動了所有東西,這樣if語句最後被執行,並且所有的東西都開始工作了。下面是完整的(功能)代碼:

<script type="text/javascript"> 

function geturl() { 
var locate = document.location 
document.fastform.myurl.value = locate 
getmeoutofhere() 
} 
window.onload = geturl; 

function getmeoutofhere() { 
    if (self.location.href == top.location.href) { 
    document.getElementById('fastform').submit(); 
    } 
} 
</script> 
<form id="fastform" name="fastform" ACTION="/amember.php" style="visibility:hidden;"> 
<INPUT TYPE="text" NAME="myurl" ID="myurl" /> 
<input type="submit" /> 
</form> 

回答

0

您可以在功能使用:

變種F = document.forms.fastform; f.submit();

它的工作完全正常

+0

此建議沒有工作...添加到我的原始文章整個程序。 –

+0

寫在上面我們的代碼,如果塊,我已經檢查它的正常工作 –

+0

我使用這個代碼: 演示 <腳本類型=」 文本/ JavaScript的「> \t函數run() \t { \t \t如果(self.location.href == top.location.href) \t \t { \t \t \t var f = document.forms.fastform; \t \t \t f.submit(); \t \t} \t} \t \t \t <表格ID = 「FASTFORM」 名稱= 「FASTFORM」 ACTION = 「/ amember.php」> \t \t \t <輸入type =「button」onclick =「run();」> \t –

0
document.getElementById('fastform').submit(); 

OR

var frm = document.getElementById('fastform'); 
frm.submit(); 
0

我不知道這是否是問題,但肯定有一個問題與線:

document.getElementById(fastform).submit(); 

問題我認爲是t您試圖通過id獲取元素的帽子,但getElementById()需要帶引號的字符串,除非您已將字符串分配給由fastform表示的變量。因此,它應該是:

document.getElementById('fastform').submit(); 

var fastform = 'fastform'; 
document.getElementById(fastform).submit(); 

而且,你似乎試圖與fastform變量去解決它似乎之前已經確定,在第一線包含if語句中:

document.fastform.submit(); 

我倒是建議修改你的腳本一點,是這樣的:

if (self.location.href == top.location.href) { 
    var fastform = document.getElementById('fastform'); 
    fastform.submit(); 
} 

參考文獻: