2013-11-24 96 views
1

我想实现一个jQuery自动完成功能类别和阿贾克斯,自动完成与类别

我有以下的javascript,我从Jquery UI website有:

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

<script type="text/javascript"> 
$(function() { 

    //autocomplete 
    $(".auto").autocomplete({ 
     source: "autocomplete.php", 
     minLength: 1, 

    });    
}); 
</script> 

和文件autocomplete.php呼应JSON数组一样这个:

[ 
{"label":"Sandy W","category":"Artist"}, 
{"label":"Shlomi Aber & Itamar Sagi","category":"Artist"}, 
{"label":"sad","category":"Keyword"} 
] 

但是,它似乎像jquery不承认的类别。当数组显示在自动完成列表中时,数组中的值不会根据它们的类别分开。

难道是Json附近的报价labelcategories?我注意到,在由jquery labelcategories提供的示例中没有引号。

感谢您的帮助:)

回答

0

由于您寻找您需要使用catcomplete这将显示的子类,而autocomplete不分类的搜索结果。

更改此:

$(".auto").autocomplete({ 

要:

$(".auto").catcomplete({ 

Demo

+1

我怎么能错过这...谢谢! :) – anto0522