我正在做http://javascriptissexy.com/how-to-learn-javascript-properly/上的「正確學習JavaScript」跟蹤。無法讓我的測驗應用程序正常工作
它花了我一直,但我終於想出瞭如何去下一個問題,但選擇不會改變。
但是,當我硬編碼「questionIndex」的問題和選擇正常工作。
反正這裏是我的代碼(我知道這是一個有點亂,我是初學者):
http://jsfiddle.net/utfwae8d/1/
HTML:
<div id="container">
<div id="quiz"></div>
<div id="choices"></div>
<input type="button" value="Next">
</div>
的JavaScript:
var allQuestions = [{
question: "Who is the best in the world?",
choices: ["CM Punk", "John Cena", "Daniel Bryan", "Roman Reigns"],
correctAnswer: 0
},
{
question: "Who is the current WWE World Champion?",
choices: ["John Cena", "Brock Lesnar", "Triple H"],
correctAnswer: 1
},
{
question: "Where is Toronto located?",
choices: ["Ontario", "California", "Georgia", "Texas"],
correctAnswer: 0
},
{
question: "What is the largest California city?",
choices: ["Los Angeles", "San Fransico", "San Deigo", "Anahiem"],
correctAnswer: 0
}];
var quiz = document.getElementById('quiz');
var choicesContainer = document.getElementById('choices');
var nextButton = document.querySelector('[type=button]');
var correctAnswers = 0;
var questionIndex = 0;
function showQuiz() {
var currentQuestion = allQuestions[questionIndex].question;
quiz.textContent = currentQuestion;
var choicesNum = allQuestions[questionIndex].choices.length;
var correctAnswer = allQuestions[questionIndex].correctAnswer;
var choices;
for (var i = 0; i < choicesNum; i++) {
choices = allQuestions[questionIndex].choices[i];
choicesHTML = "<input type='radio' name='choice'>" + choices + "</br>";
choicesContainer.innerHTML += choicesHTML;
}
nextButton.addEventListener('click', function() {
questionIndex++;
quiz.textContent = allQuestions[questionIndex].question;
});
}
showQuiz();
太棒了,謝謝! – DevonAero
@DevonAero,我編輯了代碼以使其更具可讀性。 –
真的很感激!我只是輸入了代碼,它對我來說真的很有意義。非常感謝! – DevonAero