我正在尝试使用单选按钮在 Javascript 中进行测验。我希望能够显示分数,并向用户显示他们回答错误的问题。
到目前为止,这是我的脚本:
var numQues = 4;
var numChoices = 4;
var answers = new Array(numQues)
answers[0] = "b";
answers[1] = "c";
answers[2] = "b";
answers[3] = "a";
function getScore(form) {
var score = 0;
var currQ;
var currChoice;
if(currChoice.checked) {
if(currChoice.value == answers[i]) {
score++;
break;
}
}
}
form.scoreOutofFour.value = score + "/4";
这是我的 HTML:
<body>
<div id="content">
<form name="quiz">
<table>
<tr>
<td>
<b>1.</b> Example Question 1 <br />
<input type="radio" name="q1" value="a" /> a) Example answer 1 <br />
<input type="radio" name="q1" value="b" /> b) Example answer 2 <br />
<input type="radio" name="q1" value="c" /> c) Example answer 3<br />
<input type="radio" name="q1" value="d" /> d) Example answer 4<br />
</td>
</tr>
<tr>
<td>
<b>2.</b>Example Question 2 <br />
<input type="radio" name="q2" value="a" /> a) Example answer 1 <br />
<input type="radio" name="q2" value="b" /> b) Example answer 2 <br />
<input type="radio" name="q2" value="c" /> c) Example answer 3 <br />
<input type="radio" name="q2" value="d" /> d) Example answer 4 <br />
</td>
</tr>
<tr>
<td>
<b>3.</b> Example Question 3 <br />
<input type="radio" name="q3" value="a" /> a) Example answer 1 <br />
<input type="radio" name="q3" value="b" /> b) Example answer 2<br />
<input type="radio" name="q3" value="c" /> c) Example answer 3 <br />
<input type="radio" name="q3" value="d" /> d) Example answer 4 <br />
</td>
</tr>
<tr>
<td>
<b>4.</b>Example Question 4<br />
<input type="radio" name="q4" value="a" /> a) Example answer 1 <br />
<input type="radio" name="q4" value="b" /> b) Example answer 2<br />
<input type="radio" name="q4" value="c" /> c) Example answer 3 <br />
<input type="radio" name="q4" value="d" /> d) Example answer 4 <br />
</td>
</tr>
<tr>
<td colspan="2">
<b><input type="button" value="Get score" onClick="getScore(this.form); readtheCookie();" />
<input type="reset" value="Clear" /></b>
<p>
<b>
<div id="cookie12"></div></b><br />
<textarea name="scoreOutofFour" size="15"></textarea>
<br />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>