请大家我需要有关此javascript代码的帮助。这不是我自己编写的代码,因为我还在学习Javascript。无论如何,该代码应该创建并处理在线检查并给出结果。最初,代码还不错,直到我碰到一些东西。如果只有谁能编辑代码,我会非常感激。谢谢你们。 这是代码;
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<style>
div#test{border:#000 1px solid; padding: 10px 40px;}
</style>
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB,chC, correct = 0;
var questions = [
[ "what is 10 + 4?", "12", "14", "16", "B" ],
[ "what is 20 - 9?", "7", "13", "11", "C" ],
[ "what is 7 x 3?", "21", "24", "25", "A" ],
[ "what is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test");
if(pos >= questions.length){
test.innerHTML= "<h2>you got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status").innerHTML="Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'>"+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'>"+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'>"+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++){
if(choice[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>
</html>
答案 0 :(得分:1)
您引用了选择(例如if(choice[i].checked)
),但该数组称为选择。用 choices 替换 choice 可解决此问题,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<style>
div#test{border:#000 1px solid; padding: 10px 40px;}
</style>
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB,chC, correct = 0;
var questions = [
[ "what is 10 + 4?", "12", "14", "16", "B" ],
[ "what is 20 - 9?", "7", "13", "11", "C" ],
[ "what is 7 x 3?", "21", "24", "25", "A" ],
[ "what is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test");
if(pos >= questions.length){
test.innerHTML= "<h2>you got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status").innerHTML="Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'>"+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'>"+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'>"+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choices = choices[i].value;
}
}
if(choices == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>
</html>