我正在做一个简单的 HTML5 画布练习,但遇到了一些障碍。即使在 checkAnswer 函数中返回 false 后,我也无法让页面停止刷新。我已经到处寻找答案并尝试了一些不同的东西,但似乎没有任何效果。任何帮助将不胜感激。我在这里创建了一个 JSFiddle http://jsfiddle.net/geekch1ck/ezSyf/
我也不明白 JSFiddle 上的 "{"error": "Please use POST request"}"。
预先感谢您的帮助。我确定我错过了一些相当简单的东西,但我在我浏览过的任何书籍或网站中都找不到它。
这是那些不想弄乱 JSFiddle 的人的代码
function getRandInt (min, max){
return Math.floor(Math.random()*(max-min+1)) + min;
}
function checkAnswer(guess, answer){
if (guess == answer){
alert("Correct");
setTimeout(drawText(equation),3000);
return true;
}
else{
alert("Try Again");
return false;
}
}
function drawCanvas(equation){
//draw the Canvas
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
drawText(equation);
}
function drawText(equation){
//add the text to the canvas
context.font = '40pt Veranda';
context.fillText(equation, 50, 100);
}
$(document).ready(function(){
drawCanvas(equation);
$("form").submit(function(){
guess = document.getElementById("guess").value;
checkAnswer(guess, answer);
});
});
</script>
</head>
<body>
<canvas id = "myCanvas" width = "200" height = "200" style = "border:1px solid #000000;">
</canvas>
<form action="">
<p>What is the answer? <input type = "text" id = "guess" name = "guess"></p>
</form>
</body>
</html>