使用JavaScript自动提交表单 [英] Auto-Submit Form using JavaScript
问题描述
< form name =myFormid =myFormaction =test.phpmethod =POST>
< p>
< input name =testvalue =test/>
< / p>
< p>
< input type =submitname =submitvalue =Submit/>
< / p>
< / form>
< script>
var auto_refresh = setInterval(
function()
{
submitform();
},10000);
函数submitform()
{
alert('test');
document.myForm.submit();
}
< / script>
我无法每隔 10秒自动提交表单 曾登陆网页。表单名称是myForm action =test.php
。我收到了'test'
的消息,但该页面没有提交表单。
除了自动加载函数在页面加载?
FIXED:已删除( name =submit
)从提交按钮开始,它运行得很顺利。
你需要指定一个框架,否则你的脚本会在首次提交时消失!
$ b
用文档更改
: document.myForm
。 forms [myForm]
< form name =myFormid =myForm target =_ myFrameaction =test.phpmethod =POST>
< p>
< input name =testvalue =test/>
< / p>
< p>
< input type =submitvalue =Submit/>
< / p>
< / form>
< script type =text / javascript>
window.onload = function(){
var auto = setTimeout(function(){autoRefresh();},100);
函数submitform(){
alert('test');
document.forms [myForm]。submit();
}
函数autoRefresh(){
clearTimeout(auto);
auto = setTimeout(function(){submitform(); autoRefresh();},10000);
}
}
< / script>
<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 seconds once 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.
Any solutions besides autoloading the function upon page load?
FIXED: Removed (name="submit"
) from the submit button and it worked smoothly.
You need to specify a frame, a target otherwise your script will vanish on first submit!
Change document.myForm
with 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>
这篇关于使用JavaScript自动提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!