jQuery自动完成与远程源和类别

时间:2012-08-27 14:42:03

标签: jquery asp.net json

我在这个网站上看到了与我类似的问题,但我无法弄清楚我的代码有什么问题。我正在使用jquery自动完成,它工作正常,但不显示类别。我尝试在firebug中调试,但看起来我从未在_renderMenu中找到“var self = this”

$.widget("custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function (ul, items) {
        var self = this;
        var currentCategory = "";
        $.each(items, function (index, item) {
            if (item.category != currentCategory) {
                ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
                currentCategory = item.category;
            }
            self._renderItem(ul, item);
        });
    }
});

$("#m_tSearchEngine").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: '../../Class/SearchEngine.asmx/GetSearchEngineItems',
            data: "{ 'criteria': '" + $("#m_tSearchEngine").val() + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                var c = eval(data.d);
                response($.map(c, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name,
                        id: item.Id,
                        category: item.category,
                        subCategory: item.Type,
                        secondSubCategory: item.SecondType
                    }
                }))
            },
            error: function (XMLHttpRequest, textStatus, error) {
                //alert(textStatus);
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        location.href = "../../Literature/Pages/Literature.aspx?category=" + ui.item.category + "&subCategory=" + ui.item.subCategory + "&secondSubCategory=" + ui.item.secondSubCategory;
    }
});

这就是我的json的样子:

[{"category":1,"Id":49,"Name":"4800H","Type":7,"SecondType":0},{"category":1,"Id":86,"Name":"4900H","Type":7,"SecondType":0},{"category":2,"Id":1342,"Name":"D-DA1000HP-PSM(__)DBF","Type":25,"SecondType":54}]

1 个答案:

答案 0 :(得分:1)

我终于找到了我的问题。

而不是使用

$("#m_tSearchEngine").autocomplete

我必须使用

$("#m_tSearchEngine").catcomplete