$(document).ready(function() {
allQuestions
是对象数组,其角色为我的应用程序提供问题( question
)、答案(choices
)和正确的响应( )。correctAnswer
var allQuestions = [{question: "If you were a super hero what powers would you have?",
choices: ["a", "b", "c", "d"],
correctAnswer:0},
{question: "Do you have a cherished childhood teddybear?",
choices: ["e", "f", "g", "j"],
correctAnswer:0},
{question: "Whats your favourite non-alcoholic drink?",
choices: ["i", "j", "k", "l"],
correctAnswer:0},
{question: "Are you religious?",
choices: ["m", "n", "o", "p"],
correctAnswer:0}];
接下来,一旦单击带有#next
id 的按钮,带有 id 的段落应该使用数组#question
中的下一个问题更改他的文本。allQuestions
真正的结果?当我单击下一步按钮时,该函数会遍历所有问题,并且只显示最后一个问题。
我尝试使用 stackoverflow 中的解决方案,设置 varhasLooped
但不起作用。
$('#next').click(function(){
//var hasLooped = false;
$.each(allQuestions, function(key){
// if(!hasLooped) {
$('#question').text(allQuestions[key].question);
// }
// hasLooped = true;
})
})
});