我已經實現了typeahead.js和bloodhound與遠程數據源,它大多按預期工作。但是,我將typeLhead上的minLength設置爲2,同時我可以在2個字符(和3)之後看到ajax請求觸發,而typeahead僅在4個字符或更多字符後提供建議。有沒有從我的配置丟失(我正在使用typeahead.bundle.min.js)。typeahead.js bloodhound minLength不工作
var local_authorities = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify : function(datum) {
//console.log(datum);
return datum.value;
},
remote: {
url: "/addresses/autocomplete_local_authorities/%QUERY",
wildcard: '%QUERY',
filter: function (data) {
// Map the remote source JSON array to a JavaScript object array
return $.map(data.local_authorities, function (local_authority) {
return {
id: local_authority.id,
value: local_authority.name
};
});
}
}
});
$('.typeahead.local-authority').typeahead({
hint: true,
highlight: true,
minLength: 2,
}, {
limit: 8,
source: local_authorities,
displayKey: 'value',
})
.on('typeahead:selected', function (e, item) {
$('[name="local_authority_ids"').val(item.id);
});
謝謝。
可以請你創建http://jsfiddle.net演示? – Dhiraj
謝謝。我已經建立了這個jsfiddle,但遇到了另一個問題:https://jsfiddle.net/zy3xtpzt/4/我無法獲得血腥提交郵寄請求(我本來必須解決的),所以它無法獲取數據從jsfiddle中的/ echo/json返回。 – TimSpEdge