我正在做一个简单的 HTML5 canvas 练习,但遇到了一些问题。即使在 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>
最佳答案
您是否可能没有返回 checkAnswer 的返回值?也许应该是:
$("form").submit(function() {
guess = document.getElementById("guess").value;
return checkAnswer(guess, answer);
});
关于javascript - 即使返回 false 页面也会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073782/