Javascript 通过单击重新填充对象
我试图用存储在数组中的下一个问题重新填充我的单选按钮。我不确定如何删除当前问题并用下一个问题重新填充单选按钮Javascript 通过单击重新填充对象,javascript,jquery,arrays,repopulation,Javascript,Jquery,Arrays,Repopulation,我试图用存储在数组中的下一个问题重新填充我的单选按钮。我不确定如何删除当前问题并用下一个问题重新填充单选按钮 var questionsArray = []; //Create counters for both correct answers and current question var correctAnswers = 0; var currentQuestion = 0; //Contructor Function to create questions function Ques
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;
});
$('#提交')。在('click',function()上{
当前问题++;
var answer=$('input[name=“1”]:checked').val();//通过创建answer变量,我们可以存储提交的单选按钮值。
如果(答案==问题数组[0]['answer']){
正确答案++;
$('.jumbotron').append(答案+“?
没错!在10个正确答案中,您有“+correctAnswers+”);
}否则{
$('.jumbotron').append(答案+“?
哦,天哪,那太错了!你有10个正确答案中的“+correctAnswers+”);
}
返回false;
});
我现在完全被卡住了。这里有一个你可以做的例子: 创建一个函数来填充问题和选项。添加
或
元素并更改其中的html,而不是仅使用.after()
只需确保从您的
提交处理程序中删除currentQuestion++
。我迫不及待地想重新组织调查问卷,因此我的建议如下:
var问题=[];
问题,推({
问题:“HTML代表什么?”,
选择:[
“超文本标记语言”,
“高文本主语言”,
“超译现代语言”
],
答复:0
});
问题,推({
问题:“CSS代表什么?”,
选择:[
“C-风格源”,
“级联样式源”,
“层叠样式表”
],
答复:二,
});
问题,推({
问题:“JS代表什么?”,
选择:[
“JavaSource”,
“JavaScript”,
“JavaStuff”
],
答复:一,
});
//创建问题元素
对于(var i=0;i”).addClass('choices');
对于(var n=0;n<问题[i].choices.length;n++){
var choice=$('li>')。addClass('choice');
choice.append(
$('').attr('type','radio').attr('name','question'+i).val(n).attr('id','label_question'+i+'n)
);
choice.append(
$('').attr('for','label_question'+i+'.+n).text(问题[i].选项[n])
);
选项。附加(选项);
}
问题.附加(选择);
$('问题')。附加(问题);
}
//附上答案的评估
$(“#提交”)。单击(函数(){
var result=$(“#result”);
var=0;
对于(var i=0;i
核对答案
你的循环在哪里,可以将所有问题附加到索引0中的一个问题上?你可以发布问题的完整代码ray.push(新问题)吗(…<代码> >,当你在这里,考虑发布一个JSFDDLE。我还没有添加它,这是我被困的地方。我如何/在哪里关联循环,以便它在每一次问题被回答时向前移动?这太好了!我正是想象着的。谢谢!
function populateQuestion(index) {
$('.q_question').html(questionsArray[index]['question']);
for (var i = 0; i < 4; i++) {
$('.jumbotron').html('');
$('.btn' + (i + 1)).val(questionsArray[index]['choices'][i]).prop('checked', false);
$('.label' + (i + 1)).html(questionsArray[index]['choices'][i]);
}
}
$('.continue').on('click', function() {
populateQuestion(++currentQuestion);
});