在 $.each 中附加问题
我在以下 jQuery.each()
中有 3 个输出,我想要 echo(append
) 它们中的每一个都在一个标签 li
中。
喜欢:
这是输出 $.each(): how
& 你好
& 嗨
我想要这个:
<ol>
<li>
<a href="">how</a>
</li>
<li>
<a href="">hello</a>
</li>
<li>
<a href="">hi</a>
</li>
</ol>
但是下面的jquery代码在a li中追加了全部3次,如:(我不想要这个)
<ol>
<li>
<a href="">howhellohi</a>
</li>
<li>
<a href="">howhellohi</a>
</li>
<li>
<a href="">howhellohi</a>
</li>
</ol>
这是我的jquery代码:
$.ajax({
type: "POST",
dataType: "json",
url: 'get_residence',
data: dataString_h,
cache: false,
success: function (respond) {
$.each(respond.data, function (index, value) {
$('ol li').append('<a href="" class="tool_tip" title="ok">' + value.name[index] + '</a>');
});
},
"error": function (x, y, z) {
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
这是我在jquery代码中的响应:
{
"data": [{
"name": "how",
"star_type": "5-hotel",
"site": "www.sasaas.assa",
"service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"],
"address": "chara bia paeen"
}, {
"name": "hello",
"star_type": "4-motel",
"site": "www.sasasa.asas",
"service": ["koko", "sili", "solo", "lilo"],
"address": "haminja kilo nab"
}, {
"name": "hi",
"star_type": "3-apparteman",
"site": "www.saassaas.aas",
"service": ["tv", "wan", "hamam", "kolas"],
"address": "ok"
}]
}
如何修复它?
I have 3 output in following jQuery.each()
, i want echo(append
) each a of they in one tag li
.
Like:
This is output $.each(): how
& hello
& hi
I want this:
<ol>
<li>
<a href="">how</a>
</li>
<li>
<a href="">hello</a>
</li>
<li>
<a href="">hi</a>
</li>
</ol>
But following jquery code append all 3 times in the a li, as:(i not want this)
<ol>
<li>
<a href="">howhellohi</a>
</li>
<li>
<a href="">howhellohi</a>
</li>
<li>
<a href="">howhellohi</a>
</li>
</ol>
This is my jquery code:
$.ajax({
type: "POST",
dataType: "json",
url: 'get_residence',
data: dataString_h,
cache: false,
success: function (respond) {
$.each(respond.data, function (index, value) {
$('ol li').append('<a href="" class="tool_tip" title="ok">' + value.name[index] + '</a>');
});
},
"error": function (x, y, z) {
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
This is my respond in jquery code:
{
"data": [{
"name": "how",
"star_type": "5-hotel",
"site": "www.sasaas.assa",
"service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"],
"address": "chara bia paeen"
}, {
"name": "hello",
"star_type": "4-motel",
"site": "www.sasasa.asas",
"service": ["koko", "sili", "solo", "lilo"],
"address": "haminja kilo nab"
}, {
"name": "hi",
"star_type": "3-apparteman",
"site": "www.saassaas.aas",
"service": ["tv", "wan", "hamam", "kolas"],
"address": "ok"
}]
}
How can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改
将您的代码
为因为这实际上是正确的代码。这实际上就是您使用
.each()
的原因Change
in your code to
because that's actually the correct code. That's actually why you use
.each()
我认为这就是您所追求的逻辑:
这会将数据添加到正确的列表项,而不是将相同的数据添加到所有列表项。
I think this is the logic you're after:
This will add the data to the proper list item, not the same data to all list items.
您的代码对我来说似乎是正确的,问题可能在于
value.name[index]
,它似乎总是等于howhellohi
。如果你警告value.name[index]
你会得到什么?如果你总是得到howhellohi
问题出在你的服务器端函数Your code seems correct to me, the problem might lie in
value.name[index]
which seems to be always equal tohowhellohi
. If you alertvalue.name[index]
what do you get?If you get alwayshowhellohi
the problem is in your server side function