如何格式化 Json 以实现 Jquery 的自动完成

发布于 2024-11-17 23:46:56 字数 1293 浏览 0 评论 0原文

我正在使用 RoR 和 Jquery 编写一个自动完成字段。

当我调用 getJson:

var aeropuertos;
    $.getJSON('web_services/get_airports', function(data) {
        aeropuertos = data;
    });

    $("#airports_input_origen").autocomplete({source:aeropuertos});
    $("#airports_input_destino").autocomplete({source:aeropuertos});

我得到这个:

[
    [
        "(GKA)",
        "GOROKA",
        "GOROKA",
        "PAPUA NEW GUINEA"
    ],
    [
        "(LAE)",
        "LAE",
        "PAPUA NEW GUINEA",
        "00"
    ],
    [
        "(MAG)",
        "MADANG",
        "MADANG",
        "PAPUA NEW GUINEA"
    ],
    [
        "(HGU)",
        "MOUNT HAGEN",
        "MOUNT HAGEN",
        "PAPUA NEW GUINEA"
    ],
    [
        "(LAE)",
        "NADZAB",
        "NADZAB",
        "PAPUA NEW GUINEA"
    ]
]

但似乎格式不正确,因为自动完成功能不起作用并抛出此错误:

this.source 不是一个函数

----------------- 已解决 --------------

它是一个 JSON格式问题!

而不是发送 [ ["", "" , ...] , ... ]

我必须发送:[ "......." , ".........", .. ..]

(解决方案:创建一维 Ruby 的 JSON

无论如何,谢谢您的时间! =)

I'm writting an Autocomplete field using RoR and Jquery.

When I call getJson:

var aeropuertos;
    $.getJSON('web_services/get_airports', function(data) {
        aeropuertos = data;
    });

    $("#airports_input_origen").autocomplete({source:aeropuertos});
    $("#airports_input_destino").autocomplete({source:aeropuertos});

I get this:

[
    [
        "(GKA)",
        "GOROKA",
        "GOROKA",
        "PAPUA NEW GUINEA"
    ],
    [
        "(LAE)",
        "LAE",
        "PAPUA NEW GUINEA",
        "00"
    ],
    [
        "(MAG)",
        "MADANG",
        "MADANG",
        "PAPUA NEW GUINEA"
    ],
    [
        "(HGU)",
        "MOUNT HAGEN",
        "MOUNT HAGEN",
        "PAPUA NEW GUINEA"
    ],
    [
        "(LAE)",
        "NADZAB",
        "NADZAB",
        "PAPUA NEW GUINEA"
    ]
]

But it seems that the format isn't right since the autocomplete is not working and is throwing this error:

this.source is not a function

----------------- SOLVED --------------

It was a JSON format Issue!

Instead of sending [ ["", "" , ...] , ... ]

I had to send: [ "......." , ".........", ....]

(solution: Create JSON of one dimension Ruby)

Thx anyway for your time! =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文