[英]jQuery UI - Multiple autocomplete - inconsistent results
我有以下tags.json
文件:
[
{"label" : "Aragorn"},
{"label" : "Arwen"},
{"label" : "Bilbo Baggins"},
{"label" : "Boromir"}
]
以及以下javascript代码(与工作演示相同):
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#people" ) //DIFF FROM DEMO
// 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 ) {
$.getJSON( 'tags.json', { //DIFF FROM DEMO
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;
}
});
});
</script>
但是,当我在输入框中输入例如“ ar
”时,我得到Aragorn, Arwen, Bilbo Baggins
和Boromir
。 我不知道为什么Bilbo
和Boromir
在结果中? 我应该只得到Aragorn
和Arwen
因为这些字符串包含'ar'字符串...
问题是在jQuery示例中, $.getJSON()
函数调用一些服务器端脚本,该脚本对term参数进行某些处理,即过滤名称。 对于您的情况, tags.json
文件tags.json
返回,其中包括所有结果。 如果要基于某些内容(例如输入的术语)过滤结果,则需要在调用响应(当前是$.getJSON()
函数的回调)之前应用该过滤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.