我写了下面的代码。使用此代码,按下提交按钮可手动提交表单。我还有一个计时器,我想在 10 秒后自动提交表单。但它不起作用。它一直计数到 0,然后什么都不做。你能告诉我我遗漏了什么或如何更改我的计时器(如果有,是问题所在)吗?但是我希望用户观看计时器作为我的例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function countDown(secs,elem)
{
var element = document.getElementById(elem);
element.innerHTML = "<h2>You have <b>"+secs+"</b> seconds to answer the questions</h2>";
if(secs < 1){
clearTimeout(timer);
document.getElementById('myquiz').submit();
}
secs--;
var timer = setTimeout ('countDown('+secs+',"'+elem+'")',1500);
}
</script>
<div id="status"></div>
<script type="text/javascript">countDown(5,"status");</script>
<title>Questionnaire</title>
<style type="text/css">
span {color: #FF00CC}
</style>
</head>
<body>
<h1>Please complete the following Survey</h1>
<form name="quiz" id ="myquiz" method="post" action="includes/process.php">
First Name: <input type="text" name="firstname" id="fname"/>
<p></p>
Last Name: <input type="text" name="lastname" id="lname"/>
<p></p>
<input type="submit" name="submit" value="Go"></input>
<input type="reset" value="clear all"></input>
</form>
</body>
</html>
最佳答案
产生此错误的三个错误。
- 您在表单属性中有错字。
id
后有一个空格。它应该是id="myquiz"
。 - 您的表单有一个名为“提交”的按钮,这是错误的。它覆盖了函数。将其命名为“提交按钮”或其他名称。
- 未定义“验证”方法。它应该返回 true。
对了,timeout时间不对,应该是1000。
工作示例:plunk
关于php - 使用计时器自动提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18045305/