jQuery UI -多个自动完成-不一致的结果
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()
函数调用一些服务器端脚本,该脚本使用术语参数做一些事情,即过滤名称。在您的示例中,将按原样返回tags.json
文件,其中包括所有结果。如果你想根据某些东西过滤结果,例如输入的术语,你需要在调用response(这是当前$.getJSON()
函数的回调)之前应用该过滤。
相关文章:
- 使用forEach和.shift()时结果不一致
- 将base64图像数据作为src分配给图像时,Firefox和Chrome上的结果不一致
- Meteor SmartCollection给出不一致的结果
- 返回不一致结果的日期(取决于是否存在前导零)
- Jasmine 测试在测试运行中、Firefox/Chrome 之间以及检查器开/关时的结果不一致
- 为什么结果与此装饰函数中的预期不一致
- 使用边界放置搜索请求,结果不一致
- 比较Javascript中的日期-结果不一致
- 为什么这种排序算法会在浏览器之间产生不一致的结果
- Jquery排序结果不一致
- 由于异步表单提交,结果不一致
- Javascript通配符regex搜索结果不一致
- 为什么使用按钮或Div运行查询会返回不一致的结果?
- Firefox 3.6.20 regex给出不一致的结果
- jQuery UI -多个自动完成-不一致的结果
- Javascript所见即所得文本区域插件在不同浏览器中产生不一致的结果
- 不一致的HTML5画布结果
- mongo结果与唯一字段不一致
- 不一致的 getTimezoneOffset 结果
- PHP密码哈希算法结果与Javascript结果不一致