如果我的jQuery自动填充请求返回为空,我想在下拉列表中添加“无结果”。但我到底该怎么做?
在此示例中,我删除了缩短/使代码更简单的内容:
$(element).autocomplete({
source: function (request, response) {
$.ajax({
success: function (data) {
response(data);
}
});
},
response: function(e, ui){
if (ui.content.length === 0) {
// Could I do something here?
}
},
select: function(e, ui){
// Do stuff here
return false;
}
// This is not triggered if DB returns empty
}).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return jQuery( "<li></li>" )
.data( "ui-autocomplete-item", item )
.append( "<a id='" + item.id + "'>"+ item.name + "</a>" )
.appendTo( ul );
}
答案 0 :(得分:4)
在您的回复对象中,添加以下内容:
response: function(event, ui) {
if (!ui.content.length) {
var noResult = {
value: "",
label: "No results found"
};
ui.content.push(noResult);
} else {
$("#message").empty();
}
},
<强> JSFiddle 强>