您可以使用多值演示的代码并使用正则表达式仅在括号之间插入数字:
function extractLast( term ) {
return split( term ).pop();
}
function split( val ) {
return val.split( /,\s*/ );
}
$(function() {
var availableTags = [
"Max (1)",
"Paul (3)",
"John (7)",
"Jochen (11)",
"Chris (9)"
];
$( "#tags" ) .autocomplete({
minLength: 0,
source: function( request, response ) {
// We trim the search term before comparing to the source
request.term = $.trim(request.term);
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the number between parentheses for the selected item
var num = ui.item.value.match(/\((\d+)\)/);
var value = num[1];
terms.push( value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( "," );
return false;
}
});
});
在此处查看 jsFiddle:http: //jsfiddle.net/96Y2y/2/