大廈@ friek108的出色答卷的頂部,讓我們增加了以下功能。
- 單擊外部時關閉自動填充小部件。
- 允許使用箭頭鍵滾動結果並用回車鍵選擇。
- 僅在輸入預定義的最小字符數量後才進行AJAX調用。
- 停止觸發AJAX調用的某些鍵。
這採用了@ friek108的答案,超時& ajax呼叫取消功能。你可能想先檢查一下。
ajaxAutoComplete({inputId:'autocomplete-input',ajaxUrl:'/search/my-auto-complete-results'})
function ajaxAutoComplete(options)
{
var defaults = {
inputId:null,
ajaxUrl:false,
data: {},
minLength: 3
};
options = $.extend(defaults, options);
var $input = $("#" + options.inputId);
if (options.ajaxUrl){
var $autocomplete = $('<ul id="ac" class="autocomplete-content dropdown-content"'
+ 'style="position:absolute"></ul>'),
$inputDiv = $input.closest('.input-field'),
request,
runningRequest = false,
timeout,
liSelected;
if ($inputDiv.length) {
$inputDiv.append($autocomplete); // Set ul in body
} else {
$input.after($autocomplete);
}
var highlight = function (string, match) {
var matchStart = string.toLowerCase().indexOf("" + match.toLowerCase() + ""),
matchEnd = matchStart + match.length - 1,
beforeMatch = string.slice(0, matchStart),
matchText = string.slice(matchStart, matchEnd + 1),
afterMatch = string.slice(matchEnd + 1);
string = "<span>" + beforeMatch + "<span class='highlight'>" +
matchText + "</span>" + afterMatch + "</span>";
return string;
};
$autocomplete.on('click', 'li', function() {
$input.val($(this).text().trim());
$autocomplete.empty();
});
$input.on('keyup', function (e) {
if (timeout) { // comment to remove timeout
clearTimeout(timeout);
}
if (runningRequest) {
request.abort();
}
if (e.which === 13) { // select element with enter key
liSelected[0].click();
return;
}
// scroll ul with arrow keys
if (e.which === 40) { // down arrow
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.next();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = $autocomplete.find('li').eq(0).addClass('selected');
}
} else {
liSelected = $autocomplete.find('li').eq(0).addClass('selected');
}
return; // stop new AJAX call
} else if (e.which === 38) { // up arrow
if (liSelected) {
liSelected.removeClass('selected');
next = liSelected.prev();
if (next.length > 0) {
liSelected = next.addClass('selected');
} else {
liSelected = $autocomplete.find('li').last().addClass('selected');
}
} else {
liSelected = $autocomplete.find('li').last().addClass('selected');
}
return;
}
// escape these keys
if (e.which === 9 || // tab
e.which === 16 || // shift
e.which === 17 || // ctrl
e.which === 18 || // alt
e.which === 20 || // caps lock
e.which === 35 || // end
e.which === 36 || // home
e.which === 37 || // left arrow
e.which === 39) { // right arrow
return;
} else if (e.which === 27) { // Esc. Close ul
$autocomplete.empty();
return;
}
var val = $input.val().toLowerCase();
$autocomplete.empty();
if (val.length > options.minLength) {
timeout = setTimeout(function() { // comment this line to remove timeout
runningRequest = true;
request = $.ajax({
type: 'GET',
url: options.ajaxUrl + val,
success: function (data) {
if (!$.isEmptyObject(data)) { // (or other) check for empty result
var appendList = '';
for (var key in data) {
if (data.hasOwnProperty(key)) {
var li = '';
if (!!data[key]) { // if image exists as in official docs
li += '<li><img src="' + data[key] + '" class="left">';
li += "<span>" + highlight(key, val) + "</span></li>";
} else {
li += '<li><span>' + highlight(key, val) + '</span></li>';
}
appendList += li;
}
}
$autocomplete.append(appendList);
}else{
$autocomplete.append($('<li>No matches</li>'));
}
},
complete: function() {
runningRequest = false;
}
});
}, 250); // comment this line to remove timeout
}
});
$(document).click(function() { // close ul if clicked outside
if (!$(event.target).closest($autocomplete).length) {
$autocomplete.empty();
}
});
}
}
而是通過一個附加效果的自動完成構件之一,我已經連同一個單一的,長的字符串追加所有這些,使這一進程更快。 (閱讀jQuery .append()方法here)的精彩分析。
你讓我們都處於劣勢。你可以看到你的代碼,我們不能。 – BobRodes
@BobRodes對不起,我認爲在這種情況下解釋更準確。現在你可以檢查代碼 –