输入正确答案两次后,任何后续尝试,即使再次输入正确答案,也会显示“再试一次”输出。是什么导致了这种奇怪的行为?
<!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=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var answer;
var firstNum;
var secondNum;
var check = true;
question();
function question() {
firstNum = Math.floor(Math.random() * 101);
secondNum = Math.floor(Math.random() * 101);
answer = firstNum * secondNum;
document.writeln("<h1>How much is " + firstNum + " times " + secondNum + "</h1>");
document.writeln(answer);
document.write('<input name="inputAnswer" type="text" id="inputAns"/>');
document.write('<input name="submitBtn" type="submit" value="Submit Answer" onclick="validate()"/>');
}
function validate() {
if (document.getElementById("inputAns").value == answer) {
document.write("Good");
check = true;
result();
} else {
check = false;
document.write("try again");
result();
}
}
function result() {
if (check == true)
question();
else {
document.writeln("<h1>How much is " + firstNum + " times " + secondNum + "</h1>");
document.writeln(answer);
document.write('<input name="inputAnswer" type="text" id="inputAns"/>');
document.write('<input name="submitBtn" type="submit" value="Submit Answer" onclick="validate()"/>');
}
}
</script>
</head>
<body></body>
</html>