Javascript 当迭代嵌套在另一个数组中的对象中的嵌套数组时,jQuery click方法返回undefined
为什么在for-in循环迭代第一个应答对象数组之后,click方法返回undefinedJavascript 当迭代嵌套在另一个数组中的对象中的嵌套数组时,jQuery click方法返回undefined,javascript,jquery,arrays,object,Javascript,Jquery,Arrays,Object,为什么在for-in循环迭代第一个应答对象数组之后,click方法返回undefined var i = 0; var j = 0; var allQuestions = [{question:"What is the color of the sky?",answer:["blue","yellow", "red"],correctAnswer:["blue",1]},{question:"What is the opposite of
var i = 0;
var j = 0;
var allQuestions = [{question:"What is the color of the sky?",answer:["blue","yellow",
"red"],correctAnswer:["blue",1]},{question:"What is the opposite of
up?",answer:["down","sideways", "circle"],correctAnswer:["down",1]},
{question:"What is the first number?",answer:["1","5", "7"],correctAnswer:
["1",1]}];
$(document).ready(function() {
function changeQuestion() {
$("#radios").empty();
for( answers in allQuestions[i].answer) {
var radioBtn = $('<input type="radio" class="radios" name="btnAnswers" value="'+
allQuestions[i].answer[j] + '" /><label for ="secondbtn">'
+ allQuestions[i].answer[j] + '</label>');
radioBtn.appendTo('#radios');
alert(allQuestions[i].answer[j]);
j++;
}
i++;
return true;
};
$("#nextbtn").click(function(){
changeQuestion();
});
});
var i=0;
var j=0;
var allQuestions=[{问题:“天空是什么颜色?”,回答:[“蓝色”、“黄色”,
“红色”],正确答案:[“蓝色”,1]},{问题:“蓝色的反面是什么?”
向上?,回答:[“向下”,“侧向”,“圆圈]”,正确回答:[“向下”,1]},
{问题:“第一个数字是什么?”回答:[“1”、“5”、“7”],正确答案:
["1",1]}];
$(文档).ready(函数(){
函数转换问题(){
$(“#收音机”).empty();
for(所有问题的答案[i]。答案){
var radioBtn=$(“”
+所有问题[i]。回答[j]+'';
无线电附件(“#无线电”);
警惕(所有问题[i]。回答[j]);
j++;
}
i++;
返回true;
};
$(“#nextbtn”)。单击(函数(){
改变问题();
});
});
您必须初始化变量i&j的值 将代码修改为
$(document).ready(function() {
function changeQuestion() {
$("#radios").empty();
var i=0, j=0;
for( answers in allQuestions[i].answer) {
var radioBtn = $('<input type="radio" class="radios" name="btnAnswers" value="'+
allQuestions[i].answer[j] + '" /><label for ="secondbtn">'
+ allQuestions[i].answer[j] + '</label>');
radioBtn.appendTo('#radios');
alert(allQuestions[i].answer[j]);
j++;
}
i++;
return true;
};
$("#nextbtn").click(function(){
changeQuestion();
});
$(文档).ready(函数(){
函数转换问题(){
$(“#收音机”).empty();
var i=0,j=0;
for(所有问题的答案[i]。答案){
var radioBtn=$(“”
+所有问题[i]。回答[j]+'';
无线电附件(“#无线电”);
警惕(所有问题[i]。回答[j]);
j++;
}
i++;
返回true;
};
$(“#nextbtn”)。单击(函数(){
改变问题();
});
})) 由于对象的
.answer
属性是一个数组,因此可以使用
for(var j=0; allQuestions[i].answer.length; j++) {...}
而不是
for(answers in allQuestions[i].answer) {...}
完整代码为:
$(document).ready(function() {
var i = 0;
function changeQuestion() {
$("#radios").empty();
var obj = allQuestions[i];
for(var j=0; j<obj.answer.length; j++) {
$('<input type="radio" class="radios" name="btnAnswers" value="' + obj.answer[j] + '" /><label for ="secondbtn">' + obj.answer[j] + '</label>').appendTo('#radios');
}
i++;
};
$("#nextbtn").click(changeQuestion);
});
$(文档).ready(函数(){
var i=0;
函数转换问题(){
$(“#收音机”).empty();
var obj=所有问题[i];
对于(var j=0;jj,i
和j
在哪里初始化?@Roamer-1888抱歉,没有在最初的帖子中添加它们。它们现在在那里,但仍然不起作用。@MJD我在小提琴中添加了初始化和数组,以及一个提示,显示代码如何执行步骤,但仍然不起作用。谢谢!你能解释一下吗(或提供链接)为什么在第二次运行changeQuestion时“for-in”循环返回未定义?实际上,您的for(…in…
构造不是罪魁祸首。是j
计数器一直在递增,所以在第二次运行时,它将从3开始。如果要在changeQuestion()内移动var j=0
)
,然后在每次运行开始时将其重置为零。在我的版本中,for(var j=0;…)
具有完全相同的效果。