简体   繁体   English

JavaScript-自动提交表单

[英]JavaScript - Auto submit form

I'm trying to auto-submit (login) form (datas are remebered by browser) via JavaScript on one specific webpage. 我正在尝试通过JavaScript在一个特定网页上自动提交(登录)表单(数据由浏览器重新注册)。 I managed to submit test form that I made on my PC very easy using this: 我设法使用以下方法轻松提交了在PC上制作的测试表格:

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

But when I try that on page it fails. 但是当我在页面上尝试时,它失败了。 I run JavaScript on page by Greasemonkey and it works fine, run alerts etc. but submit isn't working. 我在Greasemonkey的页面上运行JavaScript,它可以正常运行,运行警报等,但是提交不起作用。 Webpage is: http://plusklub.hteronet.ba/Login 网页是: http : //plusklub.hteronet.ba/登录

I tried to w8 for load with this part of code 我试图用这部分代码加载w8

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

Also was using this code from top comment on this question Auto-Submit Form using JavaScript Fail means nothing happens. 还在此问题的顶部注释中使用此代码, 使用JavaScript失败自动提交表单意味着什么也没有发生。

A few things I tried: 我尝试了几件事:

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

Output folowing: 输出跟随:

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

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. 通常,您需要先等待页面加载,然后再触发类似的事件,这应该可以在<body>元素中的window.loadonload事件属性起作用。

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. 我建议创建一个通过onload事件调用的函数,您可以在其中尝试检索用户详细信息(登录名和密码),如果.submit() ,则可以在try...catch异常处理下添加.submit()调用,以检查是否存在浏览器提出错误以进行进一步调试。 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. 抱歉,回答得不够准确,不能帮助您,但我希望能为您解决问题。

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

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