2
我在与一个问题的问题在代码与正确显示的选择阵列底部环。该循环的控制台日志将带回数组中的所有四个项目,所以我认为这不正确。但$(choiceList).append(choice)
行将所有四个项目四次带回,并将其用作所有四个单选按钮的文本标签。
图片
代码
<div class="answers">
<div class="form-check text-center">
<label class="form-check-label text-center">
<input class="form-check-input" type="radio" name="answer" id="answer" value="1" required>
</label>
</div>
<div class="form-check text-center">
<label class="form-check-label text-center">
<input class="form-check-input" type="radio" name="answer" id="answer" value="2">
</label>
</div>
<div class="form-check text-center">
<label class="form-check-label text-center">
<input class="form-check-input" type="radio" name="answer" id="answer" value="3">
</label>
</div>
<div class="form-check text-center">
<label class="form-check-label text-center">
<input class="form-check-input" type="radio" name="answer" id="answer" value="4">
</label>
</div>
</div>
let questions = [{
question: "This is just a test question for later. What is the answer?",
choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4"],
correctAnswer: 1
}, {
question: "This is just another test question for later. What is the answer?",
choices: ["Answer 1", "Answer 2", "Answer 3", "Answer 4"],
correctAnswer: 3
}];
let currentQuestion = 0;
let currentScore = 0;
function displayCurrentQuestion() {
let question = questions[currentQuestion].question
let questionDisplay = $('#quiz').find('.question-text')
$(questionDisplay).text(question);
let choiceList = $('#quiz').find('.form-check-label')
let numChoices = questions[currentQuestion].choices.length;
var choice;
for(let i = 0; i < numChoices; i++) {
choice = questions[currentQuestion].choices[i];
console.log(choice);
$(choiceList).append(choice);
};
};
IDS必须_unique_ – Andreas
问题是在这行'选择=问题[currentQuestion] .choices [I];'你应该考虑使用索引或唯一的ID。 –