Here is my autocomplete script for textarea
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#readers" )
// 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( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.get( "test.html", {
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: func开发者_开发技巧tion( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( "\n " );
return false;
}
});
});
The very simple test.html controller method:
@RequestMapping(value = "test.html", method = RequestMethod.GET)
public @ResponseBody String personquery(HttpServletRequest request) {
String personList = "person";
return personList;
}
Autocomplete works now, but the problem is, that suggestion list returns only one character for user to choose, in other words a list like p e r s o n
Which part in the code causes this?
Old question, but your split is the culprit
http://jsfiddle.net/mplungjan/XVccx/
try
return val.split( /[, ]/ );
精彩评论