자동 완료를 위해 source
옵션을 사용하면 request.term
길이에 따라 사용할 배열을 사용자 정의 할 수 있습니다 (사용자가 상자에 입력 한 내용).
입력이 집중 될 때 자동 완료의 search
메서드를 수동으로 호출하여 포커스가 열려 있도록해야합니다.
다음은이 코드를 작성한 것입니다. See it in action.
$(function() {
var data1 = 'abcdefghijklmnopqrstuvwxyz'.split('');
var data2 = 'cat dog fish shark unicorn sasquatch flamingo dingo snake mouse rat'.split(/\s+/g);
$('#in').autocomplete({
minLength: 0, // so it shows straight away without typing anything
source: function (request, response) {
if ((request.term || '').length <= 1)
response(filter(data1, request.term));
else
response(filter(data2, request.term));
}
}).on('focus', function() {
$(this).autocomplete('search', '');
});
function filter(array, searchTerm) {
var matchingItems = [];
for (var i = 0; i < array.length; i++) {
if (array[i].indexOf(searchTerm.toLocaleLowerCase()) >= 0)
matchingItems.push(array[i]);
}
if (matchingItems.length === 0)
return ['No Matches'];
return matchingItems;
}
});
정말 멋지다! 감사! – heroin