【问题标题】:jQuery UI/Autocomplete - How to change default variable names?jQuery UI/自动完成 - 如何更改默认变量名?
【发布时间】:2010-07-13 15:15:28
【问题描述】:

jQuery UI 自动完成使用变量名称“value”、“id”和“label”来创建下拉列表。例如:

$("#cities").autocomplete({
    source: "backend/cities.php",
    minLength: 2,
    select: function(event, ui) {
        city = ui.item.id;
        region = ui.item.label;
        country = ui.item.value;
    }
});

当我单击下拉列表中的项目时,当然会运行 select: 函数。但是,我似乎无法控制它用于填充下拉列表的变量名称 (ui.item.label)。当我在列表中单击并将值 (ui.item.value) 输入到输入字段时,同样适用。

是否有任何简单的方法可以更改用于填充下拉列表和输入字段的默认变量名称?

  • 原因是我从无法控制的远程服务器获取 JSON 数据。

  • 我不想编辑 .js,因为他们可能会定期发布 .js 的新版本。

  • 我真正想要的是能够在服务器回调和下拉菜单打开之间编辑变量及其内容。

感谢您的宝贵时间。

【问题讨论】:

  • 一个小时没有回复,我终于问了一个够奇怪的问题了吗? :)

标签: jquery json variables autocomplete


【解决方案1】:

我忘了在这里写我的答案。我想到了。自动完成代码中有一个内置的成功处理程序。有了它,您可以从服务器获取变量并将它们捆绑在一起成为正确的变量(id、标签、值)。像这样使用它:

$("#cities").autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "http://ws.geonames.org/searchJSON",
            dataType: "jsonp",
            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term
            },
            success: function(data) {
                response($.map(data.geonames, function(item) {
                    return {
                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.name
                    }
                }))
            }
        })
    },
    minLength: 2,
    select: function(event, ui) {
        // when clicked in list
    },
    open: function() {
        // triggered when the list is opened
    },
    close: function() {
        // triggered when the list is hidden
    }
});

【讨论】:

    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    相关资源
    最近更新 更多