我想将类别添加到自动完成小部件的搜索结果中,例如http://jqueryui.com/autocomplete/#categories但我无法弄清楚如何将代码集成到下面的工作代码中。
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#search" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
console.log(request);
console.log(response);
console.log(extractLast(request.term));
$.getJSON( "http://192.168.33.10/app_dev.php/search/query/" + extractLast( request.term ), {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
window.location = ("http://192.168.33.10/" + ui.item.link);
}
});
});
</script>
<div class="ui-widget">
<label for="search">Search</label>
<input type="text" id="search" />
</div>