此测验非常有效,除了我无法让它返回错误答案的 all -仅是 last 错误答案。例如,如果我为#1和#2都输入答案“ c”,则应该在底部都说两者“问题1-答案:a”和“问题2-答案:b”页面的但是,由于它是最后一个错误的答案,因此仅返回“问题2-答案:b”,因此由于某种原因,它不会返回所有反馈。
<!DOCTYPE HTML>
<html>
<head>
<title>Quiz Questions And Answers</title>
</head>
<body>
<center><h1>Quiz Questions</h1></center>
<p>
<form name="quiz">
<p>
<b>Question 1.
<br>What is the 1st letter of the alphabet?<br></b>
<blockquote>
<input type="radio" name="q1" value="a">A<br>
<input type="radio" name="q1" value="b">B<br>
<input type="radio" name="q1" value="c">C<br>
</blockquote>
<p><b>
<hr>
Question 2.
<br>What is the 2nd letter of the alphabet?<br></b>
<blockquote>
<input type="radio" name="q2" value="a">A<br>
<input type="radio" name="q2" value="b">B<br>
<input type="radio" name="q2" value="c">C<br>
</blockquote>
<p><b>
<input type="button"value="Grade Me"onClick="getScore(this.form);">
<input type="reset" value="Clear"><p>
Number of score out of 15 = <input type= text size 15 name= "mark">
Score in percentage = <input type=text size=15 name="percentage"><br>
</form>
<p>
<form method="post" name="Form" onsubmit="" action="">
</form>
<script>
var numQues = 2;
var numChoi = 3;
var answers = new Array(2);
answers[0] = "a";
answers[1] = "b";
function getScore(form) {
var score = 0;
var currElt;
var currSelection;
for (i=0; i<numQues; i++) {
currElt = i*numChoi;
answered=false;
for (j=0; j<numChoi; j++) {
currSelection = form.elements[currElt + j];
if (currSelection.checked) {
answered=true;
if (currSelection.value == answers[i]) {
score++;
break;
} else if (currSelection.value != answers[i]) {
qreturn = "Question " + (i+1) + " - Answer: " + answers[i];
document.getElementById("qreturn").innerHTML = qreturn;
//this is the incorrect answer feedback
break;
}
}
}
if (answered ===false){alert("Do answer all the questions, Please") ;return false;}
}
var scoreper = Math.round(score/numQues*100);
form.percentage.value = scoreper + "%";
form.mark.value=score;
}
</script>
<p id="qreturn"></p>
答案 0 :(得分:1)
您使用innerHTML = ...
,这意味着元素将在每次迭代时被替换。因此,必须使用+=
运算符或appendChild()
函数来显示所有答案。
固定代码
<html>
<head>
<title>Quiz Questions And Answers</title>
</head>
<body>
<center><h1>Quiz Questions</h1></center>
<p>
<form name="quiz">
<p>
<b>Question 1.
<br>What is the 1st letter of the alphabet?<br></b>
<blockquote>
<input type="radio" name="q1" value="a">A<br>
<input type="radio" name="q1" value="b">B<br>
<input type="radio" name="q1" value="c">C<br>
</blockquote>
<p><b>
<hr>
Question 2.
<br>What is the 2nd letter of the alphabet?<br></b>
<blockquote>
<input type="radio" name="q2" value="a">A<br>
<input type="radio" name="q2" value="b">B<br>
<input type="radio" name="q2" value="c">C<br>
</blockquote>
<p><b>
<input type="button"value="Grade Me"onClick="getScore(this.form);">
<input type="reset" value="Clear"><p>
Number of score out of 15 = <input type= text size 15 name= "mark">
Score in percentage = <input type=text size=15 name="percentage"><br>
</form>
<p>
<form method="post" name="Form" onsubmit="" action="">
</form>
<script>
var numQues = 2;
var numChoi = 3;
var answers = new Array(2);
answers[0] = "a";
answers[1] = "b";
function getScore(form) {
document.getElementById("qreturn").innerHTML =''
var score = 0;
var currElt;
var currSelection;
for (i=0; i<numQues; i++) {
currElt = i*numChoi;
answered=false;
for (j=0; j<numChoi; j++) {
currSelection = form.elements[currElt + j];
if (currSelection.checked) {
answered=true;
if (currSelection.value == answers[i]) {
score++;
break;
} else if (currSelection.value != answers[i]) {
qreturn = "Question " + (i+1) + " - Answer: " + answers[i];
document.getElementById("qreturn").innerHTML += '<div>'+qreturn+'</div>';
//this is the incorrect answer feedback
break;
}
}
}
if (answered ===false){alert("Do answer all the questions, Please") ;return false;}
}
var scoreper = Math.round(score/numQues*100);
form.percentage.value = scoreper + "%";
form.mark.value=score;
}
</script>
<p id="qreturn"></p>