【发布时间】:2015-12-12 23:17:02
【问题描述】:
我正在尝试使用存储在我的数组中的下一个问题重新填充我的单选按钮。我不确定如何删除当前问题并用下一个问题重新填充单选按钮。
var questionsArray = [];
//Create counters for both correct answers and current question
var correctAnswers = 0;
var currentQuestion = 0;
//Contructor Function to create questions
function Question (question, choices, answer){
this.question = question;
this.choices = choices;
this.answer = answer;
}
//Question Creations
questionsArray.push(new Question(...
要将问题附加到我的单选按钮,我使用了以下代码:
$('.q_question').append(questionsArray[0]['question']);
//In order to be able to check what radio is click you have to change to value of the radio buttons to the correct answer.
$('.btn1').after(questionsArray[0]['choices'][0]);
$('.btn1').val(questionsArray[0]['choices'][0]);
$('.btn2').after(questionsArray[0]['choices'][1]);
$('.btn2').val(questionsArray[0]['choices'][1]);
$('.btn3').after(questionsArray[0]['choices'][2]);
$('.btn3').val(questionsArray[0]['choices'][2]);
$('.btn4').after(questionsArray[0]['choices'][3]);
$('.btn4').val(questionsArray[0]['choices'][3]);
检查我得到的答案:
$('#submit').on('click', function(){
currentQuestion ++;
var answer = $('input[name="1"]:checked').val(); //By creating the answer variable we are able to store which radio button value is submitted.
if(answer == questionsArray[0]['answer']){
correctAnswers ++;
$('.jumbotron').append(answer + "?<br><br> That's correct! You have " + correctAnswers + " out of 10 correct!");
} else {
$('.jumbotron').append(answer+ "? <br><br> Oh dear, that's so so wrong! You have " + correctAnswers + " out of 10 correct");
}
return false;
});
我现在完全被卡住了。
【问题讨论】:
-
您的循环在哪里将所有问题附加到索引 0 处的一个上?
-
你能把
questionsArray.push(new Question(...的完整代码贴出来 -
当您使用它时,请考虑发布一个 jsFiddle。
-
我还没有添加它,这是我卡住的地方。我如何/在哪里关联循环,以便在每次回答问题时向前移动?
标签: javascript jquery arrays repopulation