Jquery autocomplete by categories not returning selected result
I'm trying to get the resulting listbox value from the autocomplete with categories example, and I can't get it to work like the other examples. I must be doing something stupid.
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var self = this,
currentCategory = "";
$.each( items, function( index, item ) {
if ( item.category != currentC开发者_开发技巧ategory ) {
ul.append( "" + item.category + "" );
currentCategory = item.category;
}
self._renderItem( ul, item );
});
},
/* I ADDED THIS */
select: function(event, ui){
alert(ui.item.value);
}
});
$(function() {
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ 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" ).catcomplete({
delay: 0,
source: data
});
});
</script>
I figured it out. I needed to put it here:
$("#search").catcomplete({
delay: 0,
source: data,
select: function(event, ui){
alert(ui.item.label);
}
});
精彩评论