有没有办法将一个项附加到自动完成下拉列表的末尾,该下拉菜单包含1-5个项目?我使用了open
但是因为我在加载下拉列表中添加了一个微调器,它不能正常工作。
jQuery的:
$("#search").autocomplete({
source: function(request, response) {
response([{loading: true}]);
return $.ajax({
url: "/search",
timeout: 10000,
dataType: "json",
data: {
query: request.term
},
success: function(data) {
var results;
results = [];
results = $.map(data, function(result) {
return {
label: result.name,
};
});
return response(results);
}
});
}
//open doesn't work because I have a spinning effect that opens a dropdown
//when the search is loading.
//open: function() {
// alert('test');
//}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
if(item.loading) {
return $( "<li id='spinner'></li>" )
.appendTo( ul );
}
};
谢谢!
答案 0 :(得分:4)
在将结果列表发送到push
回调之前,您应该只需response
您想要的任何项目:
$("#search").autocomplete({
source: function(request, response) {
response([{loading: true}]);
$.ajax({
url: "/search",
timeout: 10000,
dataType: "json",
data: {
query: request.term
},
success: function(data) {
var results = $.map(data, function(result) {
return {
label: result.name,
};
});
results.push({ label: "My Custom Item" });
response(results);
}
});
}
});