在 $.each 中附加问题

发布于 2024-12-07 02:10:52 字数 1986 浏览 0 评论 0原文

我在以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2024-12-14 02:10:52

更改

value.name[index] 

将您的代码

value.name

为因为这实际上是正确的代码。这实际上就是您使用 .each() 的原因

Change

value.name[index] 

in your code to

value.name

because that's actually the correct code. That's actually why you use .each()

没企图 2024-12-14 02:10:52

我认为这就是您所追求的逻辑:

$.each(respond.data, function (index, value) {
    $('ol li').eq(index).append('<a href="" class="tool_tip" title="ok">' + value.name[index] + '</a>');
});

这会将数据添加到正确的列表项,而不是将相同的数据添加到所有列表项。

I think this is the logic you're after:

$.each(respond.data, function (index, value) {
    $('ol li').eq(index).append('<a href="" class="tool_tip" title="ok">' + value.name[index] + '</a>');
});

This will add the data to the proper list item, not the same data to all list items.

阪姬 2024-12-14 02:10:52

您的代码对我来说似乎是正确的,问题可能在于 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 to howhellohi. If you alert value.name[index] what do you get?If you get always howhellohi the problem is in your server side function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文