仅在按下特殊键时显示Jquery UI自动完成 [英] Show Jquery UI Autocomplete only when a special key is pressed
本文介绍了仅在按下特殊键时显示Jquery UI自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有自动完成功能的文本框,我只想在按点键.
时显示结果.
I have a textbox with autocomplete and I want to show the results only when the dot key .
is pressed.
我尝试过:
$("#tags").on("keypress", function () {
var keys = [];
keys.unshift(e.which);
if (String.fromCharCode(keys[0]) == ".") {
} else {
$("#tags").unbind();
}
});
但是,$("#tags").unbind();
会从文本框中删除所有事件,如果再次按点键,结果将不会显示.
However, $("#tags").unbind();
removes all the events from the textbox, and if I press the dot key again, the results won't show up.
我该如何解决? 实时jsfiddle
推荐答案
以下是一种模拟Visual Studio所做的解决方案:
http://jsfiddle.net/xHy6n/2/
它存储最后一个"的位置.并将其后的所有内容用作自动填充的过滤器.
Here is a solution that emulates what visual studio does:
http://jsfiddle.net/xHy6n/2/
It stores the location of the last "." and uses anything after it as a filter for the autocomplete.
$(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"];
var lastDot = -1;
$("#tags").autocomplete({
minLength: 0,
source: function (request, response) {
if (lastDot>=0) {
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term.substring(lastDot+1))));
}
},
focus: function () {
return false;
},
select: function (event, ui) {
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = this.value.substr(0,lastDot+1);
this.value += terms.join("");
return false;
}
}).on("keypress", function (e) {
var keys = [];
keys.unshift(e.which);
if (String.fromCharCode(keys[0]) == ".") {
lastDot = $("#tags").val().length;
}
});
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
});
这篇关于仅在按下特殊键时显示Jquery UI自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文