我正在使用引导typeahead.js功能来自动完成“状态”查找功能。在提交表单之前,我怎样才能最好地防止个人输入所有的内容(并且没有成功地选择阿拉斯加)?
发布于 2014-01-03 01:37:22
Typeahead.js目前不支持这种行为。
您可以使用更适合受限选项的其他库,例如Select2。
然而,我有工作叉的打字机,支持这一点。
请参见此处的JQuery示例页面:
https://github.com/Svakinn/typeahead.js/blob/typeaheadSimple/Examples.md
发布于 2019-05-26 01:06:44
根据您的需求,您可以强制从有效选项中选择并清除任何无效的输入。
首先创建一个跟踪搜索结果的自定义source
:
var sync = false;
var current_results = [];
var current_q = '';
var selected_val = '';
function search(q, sync) {
current_q = q;
window.sync = sync;
engine.search(q, cust_sync);
}
function cust_sync(datums) {
current_results = datums;
sync(datums);
}
$('#typeahead').typeahead(
{
highlight: true,
minLength: 0
},
{
source: search // custom search engine
}
);
然后绑定select
和change
事件处理程序。
$('#typeahead').bind('typeahead:change', function(ev) {
var current_val = $('#typeahead').val();
// user moved cursor out of input without selecting from the menu
if(current_val != selected_val){
// current query has search results
if(current_results.length)
// change input to the first valid search result
$('#typeahead').typeahead('val', current_results[0].name);
// no results for this query - clear input
else
$('#typeahead').typeahead('val', '');
}
});
$('#typeahead').bind('typeahead:select', function(ev, suggestion) {
selected_val = $('#typeahead').val(); // keep track of a selected value
});
https://stackoverflow.com/questions/20819081
复制相似问题