自动完成文本区域问题
这是我的 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: function( 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;
}
});
});
非常简单的 test.html 控制器方法:
@RequestMapping(value = "test.html", method = RequestMethod.GET)
public @ResponseBody String personquery(HttpServletRequest request) {
String personList = "person";
return personList;
}
自动完成现在可以工作,但问题是,该建议列表仅返回一个字符供用户选择,换句话说,像这样的列表 p e r s 哦 n
代码中的哪一部分导致了这个问题?
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: function( 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老问题,但你的分裂是罪魁祸首
http://jsfiddle.net/mplungjan/XVccx/
尝试
return val.split( /[, ]/ );
Old question, but your split is the culprit
http://jsfiddle.net/mplungjan/XVccx/
try
return val.split( /[, ]/ );