通过Javascript代码单击HTML表单的提交按钮

问题描述:

我对WEB阻塞的了解不多,所以随时问我是否缺少任何细节.

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.

我设法填写了表单字段,但不知道如何通过JavaScript单击提交按钮.以下是原始登录代码的精简版本.如何自动单击此代码中的提交"按钮?

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>

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

或者,使用与onclick处理程序相同的代码:

or, use the same code as the onclick handler:

changeAction('submitInput','loginForm');
document.forms['loginForm'].submit();

(尽管onclick处理程序是一种愚蠢的编写:document.forms['loginForm']可以替换为this.)

(Though that onclick handler is kind of stupidly-written: document.forms['loginForm'] could be replaced with this.)