在第一个循环之前初始化一个计数器
此行循环
$.each(data[0].hotel_id, function (index, value) {
遍历 hotel_id
数组,为字典中的每个条目执行内部 JS 代码,将索引设置为字典中的键,将值设置为内容。
我的字典:
"hotel_id": [{
"hotel_id": ["1"]
}, {
"hotel_id": ["2"]
}, {
"hotel_id": ["3"]
}, {
"hotel_id": ["4"]
}],
所有项目都具有相同的索引,因此索引的值将始终是“hotel_id”,当我尝试使用它从其他字典中查找相关的额外数据时,这对我来说没有用。
我将在第一个循环之前初始化一个计数器,并在每次迭代时递增它,然后使用该值从其他数组中查找关联的额外数据。
查看完整的我的js代码: http://pastebin.com/jBWEDZrN
这是摘要从我的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", "444"],
"end_date": ["3333", "4444"],
"price_change": ["3333", "4444"]
}, {
"start_date": ["5555", "6666"],
"end_date": ["5555", "6666"],
"price_change": ["5555", "6666"]
}]
}]
我该怎么办 做?
更新:(此更新适用于上述输出 php)
我在数据库表的一行中插入了多个带有 json_encode
的(array
)值,现在想要使用 jquery 将它们作为订单进行回显。
输出的js代码是这样的:
1
你好
&11
&1,111,111
如何
&22
&2,222,222
什么
&33
&3,333,333,
1111
&1111
&1111
2222
&2222
&2222
2
精细
&44
&4,444,444
3333
&3333
&3333
4444
&4444
&4444
3
谢谢
&55
&5,555,555
好
&66
&6,666,666
5555
&5555
&5555
6666
&6666
&6666
<代码>4
This line
$.each(data[0].hotel_id, function (index, value) {
Loops through hotel_id
array executing the inner JS code for each entry in the dictionary setting index to the key in the dictionary and value to the contents.
My dictionary:
"hotel_id": [{
"hotel_id": ["1"]
}, {
"hotel_id": ["2"]
}, {
"hotel_id": ["3"]
}, {
"hotel_id": ["4"]
}],
Has all items with the same index, therefore the value of index will always be 'hotel_id', which is no use to i when i try to use it to find the relevant extra data from my other dictionaries.
I would initialise a counter before the first loop and increment it on each iteration then use this value to find the associated extra data from the other arrays.
See full my js code: http://pastebin.com/jBWEDZrN
This is summary from my js code in ajax call(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()
});
This is output php code(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", "444"],
"end_date": ["3333", "4444"],
"price_change": ["3333", "4444"]
}, {
"start_date": ["5555", "6666"],
"end_date": ["5555", "6666"],
"price_change": ["5555", "6666"]
}]
}]
What do i do?
Update:(this update is for above output php)
I insert several(array
) value with json_encode
in one row from database table, now want echo they as order with jquery.
Output js code is this:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,您的数组没有具有相同索引的项目。它具有具有不同索引的项目,但值是一个对象,其中包含每个对象具有相同名称的属性。如果数组有四个项目(如示例所示),则索引将从
0
到3
。您可以在循环中使用
value.hotel_id
来获取作为属性值的数组,例如["1"]
。如果属性中的数组仅包含一个 id,则只需使用
value.hotel_id[0]
即可获取该 id。示例:
http://jsfiddle.net/Guffa/BWLqw/
No, your array doesn't have items with the same index. It has items with different indexes, but the value is an object that contains a property with the same name for each object. The index will go from
0
to3
if the array has four items as in the example.You can use
value.hotel_id
in the loop to get the array that is the value of the property, for example["1"]
.If the array in the property only ever contains one id, you can just use
value.hotel_id[0]
to get that id.Example:
http://jsfiddle.net/Guffa/BWLqw/
我不明白使用
each
和匿名函数的意义。我只想使用 for 循环。
我仅在应用要在其他地方重用的函数时才使用每个函数。
I don't see the point of using
each
and an anonymous function.I would just use a for loop.
I only use each if I am applying a function that I would reuse elsewhere.