2012-01-15 46 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>