【发布时间】:2013-11-24 17:45:36
【问题描述】:
我正在尝试使用类别和 Ajax 实现 Jquery Autocomplete,
我有以下从Jquery UI website 获得的javascript:
<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 似乎无法识别类别。当数组中的值显示在自动完成列表中时,它们不会根据它们的类别进行分隔。
可以是 label 和 categories 周围的 Json 中的引号吗?我注意到在 jquery label 和 categories 提供的示例中,它们周围没有引号。
感谢您的帮助:)
【问题讨论】: