如果没有找到结果,我希望自动完成在其下拉列表中显示“无结果”.
我的情况就像JQuery的默认示例.
$(function() {
var availableTags = [
"ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell","Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
谢谢您的帮助.
最佳答案
以下是您可以实现此目标的一种方法:
原文链接:https://www.f2er.com/html/426380.html$(function() {
var availableTags = [ /* snip */];
var NoResultsLabel = "No Results";
$("#tags").autocomplete({
source: function(request,response) {
var results = $.ui.autocomplete.filter(availableTags,request.term);
if (!results.length) {
results = [NoResultsLabel];
}
response(results);
},select: function (event,ui) {
if (ui.item.label === NoResultsLabel) {
event.preventDefault();
}
},focus: function (event,ui) {
if (ui.item.label === NoResultsLabel) {
event.preventDefault();
}
}
});
});
基本上,您需要提供函数引用作为自动完成的源.在该函数内部,您可以使用相同的实用程序函数($.ui.autocomplete.filter)来过滤结果.然后你可以看到结果数组是否为空.如果是,则可以向结果列表添加默认消息.
我指定的其他两个选项会阻止选择或关注“无结果”选项.