我知道这个问题被问了很多次,但同样不适用于我。如果他没有在模糊空输入框中选择,我想强制用户在输入前选择结果。
我使用了类似模糊的类型,typeahead:selected。模糊正在工作,但我没有得到选定的数据并且预先输入:selected 根本不工作
请参阅此链接以获取较早的答案link1、link2和link3
下面是我的 onblur 代码
var newData = [];
$('.search').each(function() {
var idName = this.id;
var $this = $(this);
$this.typeahead({
source:function(query,process){
if( typeof searching != "undefined") {
clearTimeout(searching);
process([]);
}
searching = setTimeout(function() {
return $.getJSON(
"batchEnable.jsp?autosearch="+idName,
{ q:query },
function(data){
$.each(data, function(){
newData.push(this.name);
});
// only search if stop typing for 300ms aka fast typers
return process(newData);
});
}, 300); // 300 ms
},
}).blur(function(){
if(newData[$(this).val()] == null) {
// I am always getting this null (newData[$(this).val()]),
//if even if I select typeahead result data
$('#'+idName+'').val('');
newData = [];
}
});
});
在选择预输入数据时不会调用带有 on selected with 的代码
.on('typeahead:selected', function (obj, datum) {
console.log(datum); // datum will contain the value that was autocompleted
});