JavaScript - 自动提交表单

时间:2016-10-07 21:50:24

标签: javascript form-submit

我正在尝试通过一个特定网页上的JavaScript自动提交(登录)表单(数据由浏览器重新编号)。 我设法使用这个提交我在PC上制作的测试表格非常简单:

document.forms["formID"].submit();

但是当我在页面上尝试时,它失败了。我在页面上运行Greasemonkey的JavaScript,它工作正常,运行警报等但提交无效。 网页是:http://plusklub.hteronet.ba/Login

我尝试用这部分代码来加载w8

document.addEventListener("DOMContentLoaded", function(event) {             
    document.forms["aspnetForm"].submit();
});

也在使用此问题的最高评论Auto-Submit Form using JavaScript 失败意味着没有任何反应。

我尝试过的一些事情:

console.log("test1");
    document.addEventListener("DOMContentLoaded", function(event) { 
        console.log("test2");           
    });
    console.log("test3");
    window.onload = function () { 
        console.log("test4");       
        }
    }
    console.log("test5");
    var fo=document.getElementById("aspnetForm");
    console.log(fo);
    var bu=document.getElementsByClassName("button");
    console.log(bu);
    bu.click();
    setTimeout(function(){ console.log("test6"); }, 3000);

输出结果:

test1helloworld.user.js:13:3
test3helloworld.user.js:17:3
test5helloworld.user.js:22:3
<form name="aspnetForm" method="post" action="/Login?ReturnUrl=http://plusklub.hteronet.ba/" id="aspnetForm">helloworld.user.js:24:3
HTMLCollection [ <input#ctl04_ctl02_btnSubmit.button> ]

1 个答案:

答案 0 :(得分:0)

Unfortunately I dont't have enough rate to add a comment, so I'm going to try my shot.

Usually you need wait the page load before trigger an event like that, which supposedly window.load or onload event property in <body> element should work.

I would recommend create a function to be called through onload event where you can try retrieve the user details (login and pwd), if works, then can add your .submit() call under try...catch exception treatment to check if the browser is raising an error for further debugging. Once done, this may help you to figure out what is going on your code to not be working as expected.

Sorry, to not be so precise in the answer to help you on that, but I hope shed a light on your problem.