我需要在不使用parseInt的情况下执行此操作,但我不知道该怎么做。我试图根据a * b的实际值来检查答案。我认为错误的是我实际上无法检索文本框值。最好的方法是什么?
var answer = document.getElementById('answer');
var a = Math.floor((Math.random() * 12) + 1);
var b = Math.floor((Math.random() * 12) + 1);
document.getElementById('question').innerHTML = "What is " + a + " times " + b + "?";
function wrongRight() {
if (a * b == answer) {
document.getElementById('response').innerHTML = "Very good.";
} else {
document.getElementById('response').innerHTML = "No, please try again.";
}
}
<p id="response"></p>
<p id="question"></p>
<!-- this is where the x * x = ? will be asked-->
<p> your answer </p>
<input type="text" id="answer">
<input type="button" value="Click" onClick="wrongRight()">
答案 0 :(得分:1)
是的,正如评论中提到的那样,您将数字与DOM节点进行比较。
在您的函数wrongRight
中,将answer
替换为+answer.value
。加号将其转换为字符串中的整数。 e.g。
function wrongRight() {
if(a * b == +answer.value) {
document.getElementById('response').innerHTML = "Very good.";
} else{
document.getElementById('response').innerHTML = "No, please try again.";
}
}