模拟在 javascript 中点击提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4148875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulate clicking a submit in javascript
提问by milan
I want to be able to submit a form after a page loads automatically through Javascript.
我希望能够在通过 Javascript 自动加载页面后提交表单。
I am writing a google chrome extension.
我正在编写一个谷歌浏览器扩展。
So far the extension can fill in text in the input boxes but I can't figure out a way to get to the next step where the user would click Submit. Please help. Thank you.
到目前为止,扩展程序可以在输入框中填写文本,但我无法找到一种方法来进入用户单击提交的下一步。请帮忙。谢谢你。
EDIT:Pardon my edit. It wasn't clear that he was writing a chrome extension to those of us in the chatroom. --drachenstern
编辑:请原谅我的编辑。不清楚他是否在为聊天室中的我们编写 chrome 扩展程序。--drachenstern
回答by Mic
Did you try something like document.getElementById('myForm').submit()?
你尝试过类似的东西document.getElementById('myForm').submit()吗?
With a form like (after EDIT):
像这样的形式(编辑后):
<form id="myform" action="/url/To/Action">
...
</form>
...
<script>
document.getElementById('myForm').submit();
</script>
EDIT after your comment:
The scripttag must be after the formtag. Or at the very end of the bodytag if you want to be sure all the DOM elements are loaded.
您的评论后编辑:
该script标签必须是经过form标记。或者在body标签的最后,如果你想确保所有的 DOM 元素都被加载。
回答by Sebastien Robert
Try document.getElementById('form1').submit();
试试 document.getElementById('form1').submit();

