小编Tim*_*dge的帖子

typeahead.js猎犬minLength无法正常工作

我已经使用远程数据源实现了typeahead.js和bloodhound,并且它通常可以按预期工作。但是,我已经将typeahead的minLength设置为2,虽然我可以看到ajax请求在2个字符(和3个字符)之后触发,但是typeahead仅在4个字符或更多字符之后才提供建议。我的配置中是否缺少某些内容(我正在使用typeahead.bundle.min.js)。

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, …
Run Code Online (Sandbox Code Playgroud)

typeahead typeahead.js bloodhound

2
推荐指数
1
解决办法
3731
查看次数

标签 统计

bloodhound ×1

typeahead ×1

typeahead.js ×1