2012-01-15 44 views
0

我希望能夠自動提交表單(通用表單,供用戶跟蹤)。如何使用javascript自動提交表單(onevent)?

例如,創建一個POST到 http://www.example.com/index.php?option=track&variable=variable 應用程序/ x-WWW窗體-urlencoded有東西像

username=usernamestring 
otherdata=otherdata_string 
otherdata2=otherdata string 2 

實際字符串將預先格式化,不過,因爲所有它就像一個'平」。 它需要提交onevent,與外部js(http://example.com/scripts/js.js

我應該幹什麼?這變得很煩人。

更新:我想我並沒有真正說清楚;我有一個不應該在頁面上顯示的預製表單;它需要提交一個事件。表單域不存在於頁面上;我所做的只是鏈接到頁面上的腳本,並執行onLoad。

POST uri:http://www.example.com/index.php?option=track&variable=variable 上面的參數(option = track和variable = variable)不是表單詳細信息(postdata)。

內容類型爲application/x-www-form-urlencoded,並具有以下鍵/值。

username=usernamestring 
otherdata=otherdata_string 
otherdata2=otherdata string 2 (when encoded, the spaces get turned to %20's.) 

我需要一個腳本,在運行時提交此腳本。

+1

謝謝,希望我早點被告知。雖然有時候並不總能得到足夠的答案,但我通常會很好。 – 2012-01-15 15:40:13

+0

請參閱http://stackoverflow.com/questions/4484517/submit-form-via-javascript – erikxiv 2012-01-15 16:03:37

+0

這並沒有解決..我沒有我的答案。 – 2012-01-15 21:03:06

回答

2

您必須獲取表單對象並調用HTMLFormObject提供的submit();函數。

document.getElementById('myForm').submit(); 
+0

它需要預先格式化js,請參閱我的更新問題。 – 2012-01-15 21:03:28

0

1)以下,(當頁面被加載),則形式將立即autosubmited

<form action="post.php" name="FNAME" method="post"> 
<input type="text" name="example22" value="YOURVALUE" /> 
<input type="submit" /> 
</form> 
<SCRIPT TYPE="text/JavaScript">document.forms["FNAME"].submit();</SCRIPT> 

另一個formSubmit替代 - 提交任何腳本:

document.forms[0].submit(); 

2)或在2秒後使用按鈕點擊:

<SCRIPT TYPE="text/JavaScript">setInterval(function() {document.getElementById("myButtonId").click();}, 2000);</SCRIPT>