我想添加一个基于jQuery自动完成功能的解决方案来完成这项工作。
第1步:使列表固定高度并滚动
从https://jqueryui.com/autocomplete/获取代码 “可滚动”示例,将最大高度设置为结果列表,使其表现为选择框。
步骤2:打开重点列表:
在焦点事件上显示jQuery UI自动完成列表
步骤3:将最小字符数设置为0,以便无论输入中有多少个字符都将其打开
最后结果:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Scrollable results</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-autocomplete {
max-height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
* html .ui-autocomplete {
height: 100px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: function (request, response) {
response( availableTags);
},
minLength: 0
}).focus(function(){
$(this).data("uiAutocomplete").search('');
});
} );
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
在这里检查jsfiddle:
https://jsfiddle.net/bao7fhm3/1/