我正在进行一个简单的HTML5画布练习,并遇到了一些障碍。即使在checkAnswer函数中返回false之后,我也无法让页面停止刷新。我已经搜索了高低的答案并尝试了一些不同的东西,但似乎没有任何效果。任何帮助将不胜感激。我在这里创建了一个JSFiddle http://jsfiddle.net/geekch1ck/ezSyf/
我也不明白“{”错误“:”请在JSFiddle上使用POST请求“}”。
提前感谢您的帮助。我敢肯定我错过了一些相当简单的东西,但我在任何一本书中都找不到它。
以下是那些不想搞乱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>
答案 0 :(得分:1)
您是否可能没有返回checkAnswer的返回值?也许它应该是:
$("form").submit(function() {
guess = document.getElementById("guess").value;
return checkAnswer(guess, answer);
});