如何在 JavaScript 页面加载时自动提交表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3273154/
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
How to cause a form to be submitted automatically on page load in JavaScript?
提问by Toney
This may have an obvious answer, but I am very new to js so I have no idea how to do this. I would like a form on a page to be submitted automatically when that page is loaded without requiring any user action like pressing a button. How to do this?
这可能有一个明显的答案,但我对 js 很陌生,所以我不知道如何做到这一点。我希望页面上的表单在加载该页面时自动提交,而无需任何用户操作,例如按下按钮。这该怎么做?
回答by meder omuraliev
<form id=lol action="file.php"></form>
<script>document.getElementById('lol').submit();</script>
回答by Sarfraz
Like this:
像这样:
window.onload = function(){
document.formName.submit();
};
回答by bluesmoon
I'd go with what sAc suggests, but I'd use an event listener rather than just writing to window.onload:
我会采用 sAc 的建议,但我会使用事件侦听器,而不仅仅是写入 window.onload:
function submit_form() { document.formName.submit(); }
if(window.attachEvent){
window.attachEvent("onload", submit_form);
}else{
window.addEventListener("load", submit_form, false);
}

