jQuery中$ .each()的问题
我想通过jQuery.each
和数据hotel_id
从数组中追加一些,每个hotel_id
是4,这个循环$.each(data[0].hotel_id,... });
在自己内部运行4次内容,如果有数据库行或者插入residence_u
hotel_id
和hotel_id
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 = $(''); var $tooltip = $('').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] + '
'); }); 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] + '
'); }); 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
( 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] + '
'); }); }
在继续每个之前?