jQuery UI 自动完成功能不起作用

发布于 2024-10-27 15:11:50 字数 1964 浏览 3 评论 0原文

这是我的 JavaScript 代码:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
    $("input#suggestZams").autocomplete({
        source: "content/prevadzky/zam/zam_json2.php?letter=all",
        minLength: 1,
        delay: 0,
        select: function(event, ui) {
            alert(1);
        }
    });
});
//]]>
</script>

这是我的 HTML:

<input id="suggestZams" class="input" size="10" />

URL zam_json2.php?letter=all 返回此 json:

[
{ "id": "31440", "value": "Andrej\u010d\u00e1k, Ing." },
{ "id": "31690", "value": "Alexovi\u010d , Ing." },
{ "id": "31796", "value": "Antoni\u010d , Ing." },
{ "id": "31989", "value": "Antolik , Ing." },
{ "id": "32010", "value": "Ambrozov\u00e1 RNDr., PhD." },
{ "id": "32014", "value": "Aksam\u00edt" },
{ "id": "32024", "value": "Angelovi\u010d" },
{ "id": "32102", "value": "Andrej\u010d\u00e1k" },
{ "id": "32168", "value": "Avukov\u00e1 , Ing." },
{ "id": "32177", "value": "Andr\u00e1\u0161" },
{ "id": "32181", "value": "Andrej\u010d\u00e1kov\u00e1 , Mgr." },
{ "id": "32403", "value": "Arend\u00e1\u0161 , Bc." },
{ "id": "47379", "value": "An\u010fal" },
{ "id": "47399", "value": "Adam\u00edk , Ing." },
{ "id": "50022", "value": "Abo\u0161i" },
{ "id": "50085", "value": "Armer\u00eda Olmedo , Ing." },
{ "id": "53468", "value": "Anto\u0161" },
{ "id": "54837", "value": "Adamec , Ing." },
{ "id": "56659", "value": "Apostolou" },
{ "id": "57820", "value": "Alez\u00e1r" },
{ "id": "58576", "value": "Andrej\u010d\u00e1k , Bc." },
{ "id": "58587", "value": "Aronov\u00e1 , Ing." },
{ "id": "58595", "value": "Abaffy , Bc." },
{ "id": "58607", "value": "Adamec , Bc." },
{ "id": "58643", "value": "Antu\u0161 , Ing." },
{ "id": "62277", "value": "Adam\u010d\u00e1k , Mgr." },
{ "id": "62379", "value": "Andruch" },
{ "id": "63415", "value": "Adamkovi\u010d , Ing." }
]

This is my JavaScript code:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
    $("input#suggestZams").autocomplete({
        source: "content/prevadzky/zam/zam_json2.php?letter=all",
        minLength: 1,
        delay: 0,
        select: function(event, ui) {
            alert(1);
        }
    });
});
//]]>
</script>

This is my HTML:

<input id="suggestZams" class="input" size="10" />

The URL zam_json2.php?letter=all returns this json:

[
{ "id": "31440", "value": "Andrej\u010d\u00e1k, Ing." },
{ "id": "31690", "value": "Alexovi\u010d , Ing." },
{ "id": "31796", "value": "Antoni\u010d , Ing." },
{ "id": "31989", "value": "Antolik , Ing." },
{ "id": "32010", "value": "Ambrozov\u00e1 RNDr., PhD." },
{ "id": "32014", "value": "Aksam\u00edt" },
{ "id": "32024", "value": "Angelovi\u010d" },
{ "id": "32102", "value": "Andrej\u010d\u00e1k" },
{ "id": "32168", "value": "Avukov\u00e1 , Ing." },
{ "id": "32177", "value": "Andr\u00e1\u0161" },
{ "id": "32181", "value": "Andrej\u010d\u00e1kov\u00e1 , Mgr." },
{ "id": "32403", "value": "Arend\u00e1\u0161 , Bc." },
{ "id": "47379", "value": "An\u010fal" },
{ "id": "47399", "value": "Adam\u00edk , Ing." },
{ "id": "50022", "value": "Abo\u0161i" },
{ "id": "50085", "value": "Armer\u00eda Olmedo , Ing." },
{ "id": "53468", "value": "Anto\u0161" },
{ "id": "54837", "value": "Adamec , Ing." },
{ "id": "56659", "value": "Apostolou" },
{ "id": "57820", "value": "Alez\u00e1r" },
{ "id": "58576", "value": "Andrej\u010d\u00e1k , Bc." },
{ "id": "58587", "value": "Aronov\u00e1 , Ing." },
{ "id": "58595", "value": "Abaffy , Bc." },
{ "id": "58607", "value": "Adamec , Bc." },
{ "id": "58643", "value": "Antu\u0161 , Ing." },
{ "id": "62277", "value": "Adam\u010d\u00e1k , Mgr." },
{ "id": "62379", "value": "Andruch" },
{ "id": "63415", "value": "Adamkovi\u010d , Ing." }
]

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

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

发布评论

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

评论(3

债姬 2024-11-03 15:11:50

引用:

可以自定义自动完成功能以使其工作
借助各种数据源,只需
指定源选项。一个数据
来源可以是:

  • 包含本地数据的数组
  • 一个字符串,指定 URL
  • 回调

当使用字符串时,
自动完成插件期望
指向 URL 资源的字符串
将返回 JSON 数据。它可以在
同一主机或不同主机上
(必须提供 JSONP)。请求
参数“term”被添加到其中
网址。数据本身可以位于
与本地数据格式相同
如上所述。

你所做的事对我来说看起来很奇怪。我认为您实际上需要编辑服务器端脚本,以便它需要查询字符串变量 term 而不是字母 并且 返回一个字符串数组或 < 数组code>{label, value} 对象而不是 {value, id}。

如果 URL content/prevadzky/zam/zam_json2.php?letter=all 立即提供“完整”的单词列表,您可以按照以下方式执行操作:

$.getJSON("content/prevadzky/zam/zam_json2.php?letter=all", function(data) {
    var datacopy = $.map(data, function(item) {
        return {
            label: item.value,
            value: item.id
        };
    });
    $("input#suggestZams").autocomplete({
        source: datacopy,
        minLength: 1,
        delay: 0,
        select: function(event, ui) {
            alert(typeof ui);
        }
    });
});

Quote:

Autocomplete can be customized to work
with various data sources, by just
specifying the source option. A data
source can be:

  • an Array with local data
  • a String, specifying a URL
  • a Callback

When a String is used, the
Autocomplete plugin expects that
string to point to a URL resource that
will return JSON data. It can be on
the same host or on a different one
(must provide JSONP). The request
parameter "term" gets added to that
URL. The data itself can be in the
same format as the local data
described above.

What you are doing looks odd to me. I think that you will actually need to edit the server side script so that it expects the query string variable term instead of letter and returns an array of strings or an array of {label, value} objects instead of {value, id}.

If the URL content/prevadzky/zam/zam_json2.php?letter=all is provides the "complete" list of words at once, you can do something along these lines:

$.getJSON("content/prevadzky/zam/zam_json2.php?letter=all", function(data) {
    var datacopy = $.map(data, function(item) {
        return {
            label: item.value,
            value: item.id
        };
    });
    $("input#suggestZams").autocomplete({
        source: datacopy,
        minLength: 1,
        delay: 0,
        select: function(event, ui) {
            alert(typeof ui);
        }
    });
});
过去的过去 2024-11-03 15:11:50

如果你想从 url 获取数据,你必须为源定义一个函数:

source: function( request, response ) {
    $.ajax({url: "content/prevadzky/zam/zam_json2.php?letter=all",
            dataType: "json",
            ...
           });
    },
...

编辑:

在文档中它说:source 可以是一个 URL。在这种情况下,请尝试更改 JSON 响应,以在返回的对象中使用 'label' 而不是 'id'

If you want to get the data from a url you have to define a function for source:

source: function( request, response ) {
    $.ajax({url: "content/prevadzky/zam/zam_json2.php?letter=all",
            dataType: "json",
            ...
           });
    },
...

EDIT:

In the docs it says: source can be a URL. In this case, try to change the JSON response to have 'label' instead of 'id' in the returned objects.

橙幽之幻 2024-11-03 15:11:50

这是一个在 jQuery 1.5.1 中为我工作的脚本。

source: function( request, response ) {
    $.ajax({
        url: "...",
        dataType: "json",
        ...
        success: function( data ) {
            # data = json response
        }
    });
}

Here an Script that work for me in jQuery 1.5.1.

source: function( request, response ) {
    $.ajax({
        url: "...",
        dataType: "json",
        ...
        success: function( data ) {
            # data = json response
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文