jquery autocomplete search by lowercase

≡放荡痞女 提交于 2019-12-08 09:38:48

问题


I wanna work jquery autocomplete plugin with lowercase. The array includes uppercase, lowercase words. Bu when I write text in textbox, the jquery should convert to lowercase that text and convert the array words to lowercase then match words.

var names = [{
    value: "1",
    label: "Jon Kerer"},
{
    value: "2",
    label: "Scott MART"},
{
    value: "3",
    label: "Sel HURGE"}
];​

$("#myInput").autocomplete({
    focus: ..... ? 
    select : .... ? 
    source : ... ?
});​

回答1:


You will need to create a character map and do some parsing of the names. This is just a starter list of some characters. You could try some of the globalization libraries to locate a complete set

DEMO : http://jsfiddle.net/uQK5s/

var accentMap = {
    "á": "a",
    "ö": "o",
    "Ş": "S",
    "á": "a"
};
var normalize = function(term) {
    var ret = "";
    for (var i = 0; i < term.length; i++) {
        ret += accentMap[term.charAt(i)] || term.charAt(i);
    }
    return ret;
};

$("#test").autocomplete({
    source: function(request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        response($.grep(names, function(value) {
            value = value.label || value.value || value;
            return matcher.test(value) || matcher.test(normalize(value));
        }));
    }
});



回答2:


Use toLowerCase() to convert a string to lowercase:

var string = 'Test String';
string = string.toLowerCase(); //string is now 'test string'

I'm not sure on how you are 'matching' words but you should be able to use the same function for the value in the array, then compare/match as you wish.



来源:https://stackoverflow.com/questions/11188832/jquery-autocomplete-search-by-lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!