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。 –