使用 JavaScript 自动提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12376173/
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
Auto-Submit Form using JavaScript
提问by Crys Ex
<form name="myForm" id="myForm" action="test.php" method="POST">
<p>
<input name="test" value="test" />
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<script>
var auto_refresh = setInterval(
function()
{
submitform();
}, 10000);
function submitform()
{
alert('test');
document.myForm.submit();
}
</script>
I'm having trouble trying to auto-submit a form every 10 secondsonce landed on a page. The form name is myForm action="test.php"
. I get the 'test'
message but the page doesn't submit the form.
一旦登陆页面,我在尝试每10 秒自动提交一次表单时遇到问题。表单名称是 myForm action="test.php"
。我收到'test'
消息,但页面未提交表单。
Any solutions besides autoloading the function upon page load?
除了在页面加载时自动加载功能之外,还有其他解决方案吗?
FIXED:Removed (name="submit"
) from the submit button and it worked smoothly.
修正:name="submit"
从提交按钮中删除 ( ) 并且它工作顺利。
采纳答案by Mihai Iorga
You need to specify a frame, a target otherwise your script will vanish on first submit!
您需要指定一个框架,一个目标,否则您的脚本将在第一次提交时消失!
Change document.myForm
with document.forms["myForm"]
:
更改document.myForm
为document.forms["myForm"]
:
<form name="myForm" id="myForm" target="_myFrame" action="test.php" method="POST">
<p>
<input name="test" value="test" />
</p>
<p>
<input type="submit" value="Submit" />
</p>
</form>
<script type="text/javascript">
window.onload=function(){
var auto = setTimeout(function(){ autoRefresh(); }, 100);
function submitform(){
alert('test');
document.forms["myForm"].submit();
}
function autoRefresh(){
clearTimeout(auto);
auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
}
}
</script>
回答by jrummell
Try using document.getElementById("myForm")
instead of document.myForm.
尝试使用document.getElementById("myForm")
代替 document.myForm。
<script>
var auto_refresh = setInterval(function() { submitform(); }, 10000);
function submitform()
{
alert('test');
document.getElementById("myForm").submit();
}
</script>
回答by HNygard
A simple solution for a delayed auto submit:
延迟自动提交的简单解决方案:
<body onload="setTimeout(function() { document.frm1.submit() }, 5000)">
<form action="https://www.google.com" name="frm1">
<input type="hidden" name="q" value="Hello world" />
</form>
</body>
回答by N Czar
This solution worked for me:
这个解决方案对我有用:
<body onload="setTimeout(function() { document.myform.submit() }, 5000)">
<form action=TripRecorder name="myform">
<textarea id="result1" name="res1" value="str1" cols="20" rows="1" ></textarea> <br> <br/>
<textarea id="result2" name="res2" value="str2" cols="20" rows="1" ></textarea>
</form>
</body>
回答by Mohammad Mujtaba
Try this,
尝试这个,
HtmlElement head = _windowManager.ActiveBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = _windowManager.ActiveBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "window.onload = function() { document.forms[0].submit(); }";
head.AppendChild(scriptEl);
strAdditionalHeader = "";
_windowManager.ActiveBrowser.Document.InvokeScript("webBrowserControl");