【发布时间】:2020-10-12 14:38:31
【问题描述】:
<html>
<head>
<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
<title>Welcome to the page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>
</head>
<body>
<input type="text" id="suggestion" />
</body>
<script type="text/javascript">
$.getJSON( "data.json", function( data ) {
var data_arr = data.map(function(val){
return val.name;
})
auto(data_arr)
});
function auto(data_arr){
$('#suggestion').autocomplete({
source: data_arr
}).data("ui-autocomplete")._renderMenu = function (ul, items) {
var that = this;
var val = that.element.val();
$.each($.grep(items, function (value, key) {
return new RegExp(val.toLowerCase()).test(value.value.toLowerCase().slice(0, val.length))
}), function (index, item) {
that._renderItemData(ul, item);
});
};
}
</script>
</html>
数据.json
[
{
"id": "136599",
"name": "CE712A#BGJ"
},
{
"id": "137704",
"name": "12C#ABA"
},
{
"id": "137705",
"name": "F2212A#ABA"
}
]
以上代码适用于data.json 文件中存在的所需值。一旦输入除data.json 文件之外的任何值,建议就会弹出一点并消失。当data.json 中不存在输入时,我实际上不希望显示任何内容。请帮帮我。
【问题讨论】:
标签: javascript jquery jquery-ui autocomplete jquery-ui-autocomplete