0

我正在使用 jquery 自动完成功能将 GeoNames 中的城市加载到我页面上的文本框中:

$("#MyInput").autocomplete({
source: function (request, response) {
    $.ajax({
        url: "http://ws.geonames.org/searchJSON",
        dataType: "jsonp",
        data: {
            featureClass: "P",
            style: "full",
            maxRows: 10,
            name_startsWith: request.term
        },
        success: function (data) {
            response($.map(data.geonames, function (item) {
                return {
                    label: item.name + ", " + item.countryName,
                    value: item.name + " (" + item.countryName + ")" + " [" + item.countryCode + "]"
                };
            }));
        }
    });
}
}).data("autocomplete")._renderItem = function (ul, item) {
    return $("<li></li>")
        .data("item.autocomplete", item)
        .append("<a><strong>" + item.label + "</strong> / " + item.value + "</a>")
        .appendTo(ul);
};

此外,我正在尝试覆盖 _renderItem 以在我的自定义视图中显示自动完成结果,但此方法不影响我的项目。我的代码有什么问题?您可以在jsfiddle.net/找到示例

4

1 回答 1

1

I just did this where i wanted to set the "data" of the text box. In my case I didn't want to show any of the "data". I just "added" a third value when performing the map called data and set it to "item", then gave a "select" section. See below.

    $(document).ready(function () {
        var $birds = $("#birds")
        $birds.focus();
        mURL = "my url here";
        $birds.autocomplete({
            minLength: 0,
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    url: GW.Common.getBaseURL() + "/Functions/EmailCounts/TypeAHead.aspx/country_state_city",
                    data: "{'text':'" + request.term + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        var jsonData = $.parseJSON(msg.d)[0].Cities;
                        response($.map(jsonData, function (item) {
                            return {
                                label: item.name + ', ' + item.state + ' ' + item.country,
                                value: item.name + ', ' + item.state + ' ' + item.country,
                                data: item // added as "extra"
                            };
                        }));
                    },
                    error: function (msg) {
                        alert(msg.status + ' ' + msg.statusText);
                    }
                })
            },
            focus: function (event, ui) {
                $birds.val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                log(ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value);
                $birds.val(ui.item.label);
                $birds.attr("data", JSON.stringify(ui.item.data)); // now you can use the extra "data" you set in success.
                return false;
            }
        });
于 2013-08-16T15:11:17.047 回答