我正在尝试创建一个包含多个问题的测验页面。但是现在我在询问问题后无法从单选按钮获取值。我想通过检查特定问题的答案向用户显示答案是否正确的结果。
以下是我的代码的一部分:
的JavaScript 功能问题() {
var questions = [];
var choices = [];
var answers = [];
questions[0] = " What type of seismic activity happens in an earthquake?";
choices[0] = new Array();
choices[0][0] = " shocks";
choices[0][1] = " rays";
choices[0][2] = " waves";
choices[0][3] = " bounces";
answers[0] = choices[0][2];
var rand = array[0];
document.getElementById('q1').innerHTML = questions[rand];
document.getElementById('1a').innerHTML = choices[rand][0];
document.getElementById('1b').innerHTML = choices[rand][1];
document.getElementById('1c').innerHTML = choices[rand][2];
document.getElementById('1d').innerHTML = choices[rand][3];
var ans = answers[rand];
function check() {
var first = document.getElementsByName("fir");
var firstvalue;
for (i = 0; i < first.length; i++) {
if (first[i].checked) {
firstvalue = first[i].value;
}
}
if (firstvalue == ans) {
alert("You get the right answer!");
} else alert("Your get the wrong answer!");
}
HTML
<form name = "quiz">
Question 1 :<label id = "q1"></label><br><br>
<input type="radio" name = "fir">A) <label id="1a"></label><br><br>
<input type="radio" name = "fir">B) <label id="1b"></label><br><br>
<input type="radio" name = "fir">C) <label id="1c"></label><br><br>
<input type="radio" name = "fir">D) <label id="1d"></label><br><br>
<input type = "button" id = "submitbtn" value = "Submit" onClick = "check()">
</form>