如果你看到这个Fiddle 演示,不是我做的,那么我怎样才能避免键盘向下并选择禁用的元素?鼠标工作正常(无法选择它)但我可以用键盘向下并选择它,导致搜索为空:-/
Fiddle 演示来自这篇文章,How to disable element in jQuery autocomplete list
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,
response: function (event, ui) {
if (ui.content.length > 3) {
while (ui.content.length > 3) {
ui.content.pop();
}
ui.content.push({
'label': 'Please narrow down your search',
'value': ''
});
}
}
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li " + (item.value == '' ? 'class="ui-state-disabled"' : '') + ">")
.append("<a>" + item.label + "</a>")
.appendTo(ul);
};
});