正在迭代jquery对象
Iterating jquery objects
我正试图使用jqueryeach()迭代并输出choices属性的值。我的代码似乎输出的是数组索引,而不是字符串值。为什么会这样?这是我的jsfiddle
var allQuestions = [
{
question: "Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0
}];
$(info[0].choices).each(function(value){
$('#answers').append($('<li>').text(value));
});
您混淆了
$(selector).each()
http://api.jquery.com/each/
带有
$.each(array, callback)
http://api.jquery.com/jquery.each/
您希望使用第二种形式对数组进行迭代。
您传递的是索引而不是值:
var allQuestions = [{
question: "Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0
}];
$(info[0].choices).each(function(index, value){
$('#answers').append($('<li>').text(value));
});
下面是一个小提琴的例子:http://jsfiddle.net/436Lcvc4/
试试这个
$(info[0].choices).each(function(value){
$('#answers').append($('<li>').text(this));
$.each
回调接受两个参数index
和value
。添加value
作为第二个参数,然后使用该参数。
相关文章:
- 如何删除对象数组中的对象:jquery
- 为一个声音元素数组创建一个音频对象jquery,javascript
- 在原型对象 jquery 上将一个按钮与另一个按钮不同
- 编辑对象 jQuery 的属性
- ajax 正在用多个调用覆盖 XHR 对象 - jQuery
- 传递值并将其显示为对象 - jquery
- 如何将变量名转换为对象jQuery的索引字符串
- 访问多个动态对象 jQuery
- 使用OptGroup从两个分离的JSon对象JQuery填充Select
- 显示对象jquery中的数据
- 清空Javascript对象(Jquery Mobile)
- null不是对象-jQuery+传单
- 当事件是由单元格中的对象jQuery引起时,如何更改该单元格的自定义属性
- 未捕获类型错误:Property '$'对象- jQuery
- 从数组对象Jquery中移除对象
- 将属性值转换为对象(jquery或javascript)
- 不知道如何排序这个JSON对象jQuery/JavaScript
- 解析和字符串化json对象jquery
- 插入对象(jquery)延迟
- HTML ' img '标签不能在对象jquery中显示为图像