简体   繁体   English

如何使用javascript自动提交表单(onevent)?

[英]How can I submit a form automatically (onevent) with javascript?

I'd like to be able to submit a form automatically on an event ( a generic form, for user tracking). 我希望能够在事件中自动提交表单(用于用户跟踪的通用表单)。

For example, create a POST to http://www.example.com/index.php?option=track&variable=variable application/x-www-form-urlencoded with stuff like 例如,使用以下内容创建到http://www.example.com/index.php?option=track&variable=variable application / x-www-form-urlencoded的POST

username=usernamestring
otherdata=otherdata_string
otherdata2=otherdata string 2

The actual string will be preformatted, though, because all it is is like a 'ping'. 但是,实际的字符串将被预先格式化,因为它就像一个“ ping”。 It needs to be submitted onevent, with external js ( http://example.com/scripts/js.js ) 需要使用外部js( http://example.com/scripts/js.js )在事件提交时提交

What the hay should I do? 我应该做什么干草? This is getting annoying. 这很烦人。

Update: I guess I didn't really make myself clear; 更新:我想我并没有说清楚。 I have a premade form that isn't supposed to display on the page; 我有一个预制的表格,该表格不应该显示在页面上; it needs to submit on an event. 它需要提交一个事件。 The form fields do not exist on the page; 表单字段在页面上不存在。 all I do is link to the script on the page and it executes onLoad. 我要做的就是链接到页面上的脚本,它执行onLoad。

POST uri: http://www.example.com/index.php?option=track&variable=variable The arguments above (option=track and variable=variable) are not the form details (postdata). POST uri: http : //www.example.com/index.php ?option=track&variable=variable上面的参数(option = track和variable = variable)不是表单详细信息(postdata)。

The content type is application/x-www-form-urlencoded , and has the following keys/values. 内容类型为application / x-www-form-urlencoded,并且具有以下键/值。

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

I need a script that submits this when run. 我需要一个在运行时提交此脚本的脚本。

You have to get the form object and call the submit(); 您必须获取表单对象并调用submit(); function provided by HTMLFormObject . HTMLFormObject提供的功能。

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

1) with the following, (while page is loaded), the form will be immediately autosubmited 1)使用以下命令,(在页面加载时),将立即自动提交表单

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

another formSubmit alternative - submits any script: 另一种formSubmit替代方案-提交任何脚本:

document.forms[0].submit();

2) or use button click after 2second: 2)或2秒后使用按钮单击:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM