[英]Problem with $.each() in jQuery
我想通过jQuery.each
和数据hotel_id
从数组中追加一些,每个hotel_id
数量为4,然后循环$.each(data[0].hotel_id,... });
在self内运行4次内容,以防在数据库行residence_u
和residence_p
中存在或插入3次(每次都有3次,而不是4行(例如hotel_id
)),运行以下代码时,此代码没有错误jus不行。 如果我删除代码$.each(info_ru.units,... });
和$.each(info_rp.start_date,... });
它的工作原理:如何解决?
查看完整的我的js代码:[ http://pastebin.com/jBWEDZrN ] [1]
这是我在ajax调用中的js代码的摘要( url: 'get_gr',
):
$.each(data[0].hotel_id, function (index, value) {
var $li = $('<li><input name="hotel_id[]" value="' + value.hotel_id + '" style="display:none;"></li>');
var $tooltip = $('<div class="tooltip"></div>').appendTo($li);
$li.appendTo('#residence_name');
var info_ru = data[0].residence_u[index];
$.each(info_ru.units, function (index, value) {
$tooltip.append(value + ' & ' + info_ru.extra[index] + ' & ' + info_ru.price[index] + '<br>');
});
var info_rp = data[0].residence_p[index];
$.each(info_rp.start_date, function (index, value) {
$tooltip.append(value + ' & ' + info_rp.end_date[index] + ' & ' + info_rp.price_change[index] + '<br>');
});
tool_tip()
});
这是输出的php代码( url: 'get_gr',
):
[{
"guide": null,
"hotel_id": [{
"hotel_id": ["1"]
}, {
"hotel_id": ["2"]
}, {
"hotel_id": ["3"]
}, {
"hotel_id": ["4"]
}],
"residence_u": [{
"units": ["hello", "how", "what"],
"extra": ["11", "22", "33"],
"price": ["1,111,111", "2,222,222", "3,333,333"]
}, {
"units": ["fine"],
"extra": ["44"],
"price": ["4,444,444"]
}, {
"units": ["thanks", "good"],
"extra": ["55", "66"],
"price": ["5,555,555", "6,666,666"]
}],
"residence_p": [{
"start_date": ["1111", "2222"],
"end_date": ["1111", "2222"],
"price_change": ["1111", "2222"]
}, {
"start_date": ["3333", "4444"],
"end_date": ["3333", "4444"],
"price_change": ["3333", "4444"]
}, {
"start_date": ["5555", "6666"],
"end_date": ["5555", "6666"],
"price_change": ["5555", "6666"]
}]
}]
更新:
我在数据库表的一行中用json_encode
插入了json_encode
( array
)值,现在想用jquery将它们按顺序回显。
输出js代码是这样的:
1
hello
&11
&1,111,111
how
&22
&2,222,222
what
&33
&3,333,333,
1111
&1111
&1111
2222
&2222
&2222
2
fine
&44
&4,444,444
3333
&3333
&3333
4444
&4444
&4444
3
thanks
&55
&5,555,555
good
&66
&6,666,666
5555
&5555
&5555
6666
&6666
&6666
4
您的Residence_u数组中只有3个元素,而Residence_P数组中只有2个元素,而原始酒店数组中只有4个元素。 就像检查有效的info_ru一样简单吗
var info_ru = data[0].residence_u[index];
if ( infor_ru != null ) {
$.each(info_ru.units, function (index, value) {
$tooltip.append(value + ' & ' + info_ru.extra[index] + ' & ' + info_ru.price[index] + '<br>');
});
}
在继续每个之前?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.