typeahead js accent折叠,不敏感的口音

时间:2014-09-15 15:50:52

标签: javascript json jquery-autocomplete typeahead.js accent-insensitive

我正在使用typeahead,我跟着hereTypeahead insensitive accent但我无法成功,我不明白我的错在哪里?

这是我的js

        var charMap = {
        "à": "a",
        "á": "a",
        "â": "a",
        "é": "e",
        "è": "e",
        "ê": "e",
        "ë": "e",
        "É": "e",
        "ï": "i",
        "î": "i",
        "ô": "o",
        "ö": "o",
        "û": "u",
        "ù": "u",
        "ü": "u",
        "ñ": "n"    
    };

    var normalize = function (input) {
        $.each(charMap, function (unnormalizedChar, normalizedChar) {
            var regex = new RegExp(unnormalizedChar, 'gi');
            input = input.replace(regex, normalizedChar);
        });
        return input;
    };

    var queryTokenizer = function (q) {
        var normalized = normalize(q);
        return Bloodhound.tokenizers.whitespace(normalized);
    };

        var tels = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: queryTokenizer,
            limit: 10,
            prefetch: {
                url: 'assets/tels.json',
                filter: function (name) {
                    return $.map(tels,  function (name) {
                        // Normalize the name - use this for searching
                        var normalized = normalize(name);
                        return {
                            value: normalized,
                            // Include the original name - use this for display purposes
                            displayValue: name
                        };
                    })
                }
                }
        });
    tels.initialize();
 $('#the-basics').typeahead({
                hint: true,
                highlight: true,
                minLength: 2
            },
            {
                name: 'tels',
                displayKey: 'displayValue',
                source: tels.ttAdapter()
            });

非常感谢

0 个答案:

没有答案