2

我当前的代码使用的是 jQuery 1.7.x。我想升级到 jQuery 1.8,但我的自动完成功能遇到了问题。我用相同的代码创建了 2 个 jsfiddles。在一个示例中,它可以正常工作,而在另一个示例中则不能。主要问题是当您搜索并单击时,应该显示一个警告框并说明单击了什么。它适用于旧的 jQuery,但不适用于 1.8。

这是工作的 1.7 版本 - http://jsfiddle.net/u2GEe/1/

这是损坏的 1.8 版本 - http://jsfiddle.net/TPWXh/3/

这是代码:

$.widget("custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
        var self = this,
            currentCategory = "";
        $.each(items, function(index, item) {
            if (item.category != currentCategory) {
                ul.append("<li class='ui-autocomplete-category search-dropdown-category'>" + item.category + "</li>");
                currentCategory = item.category;
            }
            self._renderItem(ul, item);
        });
    }
});
$(document).ready(function() {
var data = [
    {
    label: "anders",
    category: "aa"},
{
    label: "andreas",
    category: "aa"},
{
    label: "antal",
    category: "aa"},
{
    label: "annhhx10",
    category: "Products"},
{
    label: "annk K12",
    category: "Products"},
{
    label: "annttop C13",
    category: "Products"},
{
    label: "anders andersson",
    category: "People"},
{
    label: "andreas andersson",
    category: "People"},
{
    label: "andreas johnson",
    category: "People"}
];

$("#search_input").catcomplete({
    source: data,
    select: function(event, ui) {
        alert(ui.item.label);
    }
});


});​

有谁知道这笔交易是什么?

4

2 回答 2

5

在花了几天时间之后,我终于弄明白了。这是一行:

 self._renderItem(ul, item);

现在需要

self._renderItemData(ul, item);

在我改变它之后,一切都恢复正常了。非常令人沮丧。

于 2012-11-07T19:45:39.647 回答
0

通过记录select回调中的值,我注意到原来itemundefined.

$("#search_input").catcomplete({
    source: data,
    select: function(event, ui) {
        console.log(event, ui); // DEBUG
        alert(ui.item.label);
    }
});

不知道为什么 jQuery 1.8.2 和 jQuery UI 1.9.1 不能一起工作,但我通过使用 jQuery UI 1.8.2而不是 1.9.1 让它工作。

在这里试试。

于 2012-11-07T19:43:59.740 回答