如果用户在文本框中键入内容然后清除它,然后再次开始输入,则Typeahead源不会更新
这是我的代码
<input id="docSearch" type="text" autocomplete="off" placeholder="Search Doctor..." >
$('#docSearch').keyup(function(){
var list;
var q = $('#docSearch').val();
if(!q){
return;
}
$.ajax({
type: "POST",
url: "pullDocs.php",
data: {inp:q},
success: function(data){
//Typeahead accepts only proper json
list = $.parseJSON(data);
$('#docSearch').typeahead({
source: list
})
}
});
});
答案 0 :(得分:0)
我如下更改了我的js代码(只需替换了keyup函数),然后效果很好
$(document).ready(function () {
$('#docSearch').typeahead({
source: function (query, result) {
$.ajax({
url: "pullDocs.php",
data: 'inp=' + query,
dataType: "json",
type: "POST",
success: function (data) {
result($.map(data, function (item) {
return item;
}));
}
});
}
});
});