通过 Javascript 代码单击 HTML 表单的提交按钮
我对 WEB 编程了解不多,所以请随时询问我是否遗漏了任何细节。
有一个我经常访问的网站,它要求用户每次访问时都需要登录。对于这个网站的登录页面,我试图写下一个用户脚本,它将自动登录。
我设法填写了表单字段,但不知道如何通过 JavaScript 单击提交按钮。以下是原始登录代码的精简版本。如何在这段代码中自动点击这个提交按钮?
<div id="start">
<div id="header">
<div id="login">
<form id="loginForm" name="loginForm" method="post" action="#">
// ...
<input type="submit" id="loginSubmit" onclick="changeAction('submitInput','loginForm');document.forms['loginForm'].submit();" value="Log in" />
// ...
</form>
</div>
</div>
</div>
I don't know much about WEB probramming, so feel free to ask if I'm missing any details.
There is a certain website which I'm visiting very frequently, and it requires users to log in every time they visit. For the login page of this website, I'm trying to write down a userscript which will automatically log me in.
I managed to fill in the form fields, but don't have any idea how to click the submit button by JavaScript. The below is a condensed version of the original login code. How can I automatically click this submit button in this code?
<div id="start">
<div id="header">
<div id="login">
<form id="loginForm" name="loginForm" method="post" action="#">
// ...
<input type="submit" id="loginSubmit" onclick="changeAction('submitInput','loginForm');document.forms['loginForm'].submit();" value="Log in" />
// ...
</form>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,提交表单的常用方法是在表单本身上调用submit(),如 krtek 的答案中所述。
但是,如果您出于某种原因需要实际单击提交按钮(您的代码取决于发布的提交按钮的名称/值或其他内容),您可以单击提交按钮本身,如下所示:
The usual way to submit a form in general is to call submit() on the form itself, as described in krtek's answer.
However, if you need to actually click a submit button for some reason (your code depends on the submit button's name/value being posted or something), you can click on the submit button itself like this:
或者,使用与
onclick
处理程序相同的代码:(尽管
onclick
处理程序的编写有点愚蠢:document.forms['loginForm'] 可以替换为
this
。)or, use the same code as the
onclick
handler:(Though that
onclick
handler is kind of stupidly-written:document.forms['loginForm']
could be replaced withthis
.)您可以这样做:
但这不会调用按钮的
onclick
操作,因此您需要手动调用它。请注意,您必须使用表单的
名称
而不是id
来访问它。You can do :
But this won't call the
onclick
action of your button, so you will need to call it by hand.Be aware that you must use the
name
of your form and not theid
to access it.