jQuery UI Autocomplete无匹配结果提示的问题
各位好,最近在用在学习用jQuery UI Autocomplete,看完了API,好像没有“无匹配结果”时的提示功能。于是我尝试自己写,思路是get返回时判断结果,如果结果空,则向ul返回一个提示文字,但效果并不好,一是我希望提示是html的,这样我可以自定义提示样式。二是我希望他不可选(一个提示)。
各位有什么好办法,指点一下。谢谢
我的代码:
$(this).autocomplete({
source: function (request, response) {
var term = request.term;
if (term in cache) {
response(cache);
return;
};
$.getJSON("/asset/php/laobao/getSearch.php", request, function (data, status, xhr) {
if(data.length>0){
cache = data;
response(data);
}else{
response(['无匹配数据']);
};
});
}
})
官方demo地址:https://www.runoob.com/try/try.php?filename=jqueryui-example-autocomplete-remote-with-cache
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI 自动完成(Autocomplete) - 远程缓存</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" >
<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
}
</style>
<script>
$(function() {
var cache = {};
$( "#birds" ).autocomplete({
minLength: 2,
source: function( request, response ) {
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.getJSON( "search.php", request, function( data, status, xhr ) {
cache[ term ] = data;
response( data );
});
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="birds">鸟:</label>
<input id="birds">
</div>
</body>
</html> 意思是想自己弄个提示层? 我自己写出了一个笨方法如下,功能基本达到预期。请大家指正。
$.getJSON("/asset/php/laobao/getSearch.php", request, function (data, status, xhr) {
if(data.length>0){
cache = data;
response(data);
}else{
response(['null']);
$(".ui-autocomplete").html('<span style="margin: 0 10px"><i class="fas fa-exclamation"></i> 无匹配结果</span>');
};
});
效果:
页:
[1]