[英]Issue with jQuery append
我想在一个复选框中动态显示答案。
var multiQ= new Array();
var questionIndex = 0;
function updateQuestion(direction) {
questionIndex += direction;
if (questionIndex <multiQ.length && questionIndex >= 0) {
$("#question").html(multiQ[questionIndex].question);
$.each(multiQ[questionIndex].answers, function(key, val) {
$('#answers').append('<label><input type="checkbox" value="key">' + val + '</label>');
});
}else {
questionIndex -= direction;
}
}
$(document).ready(function(){
$.getJSON("data.json", function(json) {
function Question(q,correactA,array){
this.question=q;
this.correct_a=correactA;
this.answers=array;
}
multiQ= new Array();
for (i=0;i<5;i++){
var q = json.questions[i].question;
var corA= json.questions[i].correct_answer;
var a = json.questions[i].answers;
var aString = "";
Object.keys(a).forEach(function (k) {aString += a[k] ;})
multiQ[i]=new Question(q,corA,a);
}
updateQuestion(0);
});
});
问题可以很好地动态显示,但是我的答案有问题。 新答案将添加到先前答案的末尾,而不是替换它们。 我应该使用哪个命令而不是追加?
您应该改用.html()
。
要实现没有jQuery,请使用类似以下内容的document.getElementById('MyDiv').innerHTML = 'txt'
: document.getElementById('MyDiv').innerHTML = 'txt'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.