jQuery UI 自动完成将术语划分为单独的字符,而不是单词
我正在编写一个 ASP.NET MVC 网站,我试图使用 jQuery UI 自动完成功能实现自动完成功能。
我的页面设置方式如下:
我有一个 id 为 TagInput
的文本框。接下来,我有以下内联 Javascript 代码:
$().ready(function () {
bindAutoTagComplete('#TagInput');
});
除了引用该页面上的 jQuery 和 jQuery UI 库之外,我还引用了此外部 Javascript 代码:
function bindAutoTagComplete(item, otherRootDomain)
{
var url = (otherRootDomain || "") + "/tags/ajaxList";
function split( val ) {
return val.split(' ');
}
function extractLast( term ) {
return split( term ).pop();
}
$(item).autocomplete({
source: function( request, response ) {
$.getJSON(url, {
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( " " );
return false;
}
});
}
一切正常,除了带有标签名称的 AJAX 结果分为单个字符,而不是单词。 例如,如果我返回两个标签“foo”和“bar”,则弹出的自动完成列表会列出:
- f
- o
- o
- b
- a
- r
而不是:
- foo
- bar
I'已经调试了代码,但还没有找到导致这种错误划分的原因。有什么想法吗? 提前致谢。
更新:以下是服务器当前在该 AJAX 请求中返回的内容的示例:
“foo bar 其他标签”
I'm writing an ASP.NET MVC site where I'm trying to implement an autocomplete function using the jQuery UI autocomplete feature.
Here's how my page is setup:
I have a textbox with id TagInput
. Next, I have the following inline Javascript code:
$().ready(function () {
bindAutoTagComplete('#TagInput');
});
In addition to referencing the jQuery and jQuery UI libraries on that page, I also reference this external Javascript code:
function bindAutoTagComplete(item, otherRootDomain)
{
var url = (otherRootDomain || "") + "/tags/ajaxList";
function split( val ) {
return val.split(' ');
}
function extractLast( term ) {
return split( term ).pop();
}
$(item).autocomplete({
source: function( request, response ) {
$.getJSON(url, {
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( " " );
return false;
}
});
}
Everything works fine, except that the AJAX result with tag names is divided into single characters, not words. For example, if I have two tags "foo" and "bar" that are returned, the autocomplete list that pops up lists:
- f
- o
- o
- b
- a
- r
instead of:
- foo
- bar
I've debugged the code, but haven't found what is causing this incorrect division. Any ideas? Thanks in advance.
UPDATE: Here's a sample of what's currently being returned by the server in that AJAX request:
"foo bar some-other-tag"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题很可能是
split
函数,它看起来像是一些自定义的 split 函数。很难说,因为这里没有所有相关代码。The problem is most probably the
split
function which looks like it's some custom split function. It's hard to say since there's not all relevant code here.谢谢你们的提示,伙计们!我发现了问题;事实上,有两个:
现在可以了!
Thanks for your tips, guys! I found the problem; in fact, there were two:
It works now!