这是一个模拟 Visual Studio 所做的解决方案:http:
//jsfiddle.net/xHy6n/2/
它存储最后一个“。”的位置。并使用它之后的任何内容作为自动完成的过滤器。
$(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();
}
});