在https://raw.github.com/michaelcox/bootstrap/6789648b36aedaa795f1f5f11b4da6ab869f7f17/js/bootstrap-typeahead.js获取最新的 bootstrap typeahead 插件 v2.1.2
此更新将允许 minLength 为零以启用 show all for typeahead
<input id="typeaheadField" name="typeaheadField" type="text" placeholder="Start Typing">
$("#typeaheadField").typeahead({
minLength: 0,
items: 9999,
source: ["Alabama","Alaska","Arizona","Arkansas","California","Colorado", "Oregon"]
});
然后您必须将 onFocus 事件附加到您的元素,因为它不是由插件定义的:
$("#typeaheadField").on('focus', $("#typeaheadField").typeahead.bind($("#typeaheadField"), 'lookup'));
如果结果太多,在本地覆盖 bootstrap typeahead css 类以设置结果的最大高度和垂直滚动也是一个好主意。
.typeahead {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
}