我的表单中有一个简单的文本输入,我想针对特定的单词进行验证。它非常简单,但表单不是由我的代码处理的,在提交时它没有做任何事情:
这是表单的HTML。
<div id="wrapper">
<section><p id="showtime"></p></section>
<section><p id = "quest"></p></section>
<p id = "fieldA"></p>
<form method="post" action="" name="AnsForm" onsubmit="return checkForm()">
<label class="choice" for="answer">Your Answer</label><br/>
<input type="text" name="answer"></input>
</form><br/>
<input type="submit" value="Submit">
<div id="footer">
<p>User1234</p>
<p>Level: </p><p id="level"></p>
</div>
</div>
而JavaScript不起作用的功能是checkForm()
<script language="JavaScript">
var difficulty = "easy";
var question = "Name of the only mammal that lays eggs";
document.getElementById("quest").innerHTML = question;
function startTime() {
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('showtime').innerHTML = h+":"+m+":"+s;
var t = setTimeout(function()
{startTime()},500);
}
function checkTime(i) {
if (i<10) {
i = "0" + i;
} // add zero in front of numbers < 10
return i;
}
function checkForm() {
var answer = "platypus";
var ansrs = document.forms["AnsForm"]["answer"].value;
var ansrL = ansr.toLowerCase();
if (ansrL == answer) {
document.getElementById("fieldA").innerHTML = "Your answer is correct!";
}
else {
document.getElementById("fieldA").innerHTML = "Sorry, try again!";
return false;
}
}
</script>
答案 0 :(得分:0)
几乎没有更正:
var ansrs = document.forms["AnsForm"]["answer"].value;
var ansrL = ansr.toLowerCase();
^^^^ wrong spelling, it is - ansrs
此外,提交按钮需要包含在表单标记中:
</form><br/>
<input type="submit" value="Submit"> <!-- move it inside form tag -->