我正在尝试实现自定义的renderItem方法,而不是获取任何自动完成结果。我想在列表中包含标签和值,并将标签对齐到右侧。这是我到目前为止所尝试的内容。
ajax.js
SSD300
HTML:
function autoCompleteResults(str) {
$.ajax({
type: 'GET',
url: 'includes/autocomplete.php?q=' + str,
data: '{}',
dataType: 'json',
success: function(data) {
console.log(data);
$('#search-input').autocomplete({
source: data,
minLength: 2,
delay: 500
}).data('ui-autocomplete')._renderItem = function(ul, item) {
return $('<li></li>').data('item.ui-autocomplete', item)
.append('<a>' + item.value + '<span class="ac-label">' + item.label + '</span></a>')
.appendTo(ul);
};
}
});
}
PHP:
<div class="search">
<form method="get" action="index.php">
<label class="search-label">Enter search value:</label>
<input id="search-input" class="search-input" type="text" name="searchvalue" onkeyup="autoCompleteResults(this.value);" autocomplete="off">
<button class="search-input-btn" type="submit" name="search" value="1">Search</button>
</form>
</div>