jQuery Autocomplete with Special Characters (i.e. ö, Ä, é, or ß)

后端 未结 3 1319
旧巷少年郎
旧巷少年郎 2021-01-19 18:50

How can I have autocomplete match on words with special characters, such as those in German: ö, Ä, é, or ß. For example, I\'d like \"mun\" to match \"München\" and \"Munchen

3条回答
  •  爱一瞬间的悲伤
    2021-01-19 19:03

    jQuery UI has a demo for this problem (accent folding) on their site: https://jqueryui.com/autocomplete/#folding

    $(function() {
      var names = ["Jörn Zaefferer", "Scott González", "John Resig"];
    
      var accentMap = {
        "á": "a",
        "ö": "o"
      };
      var normalize = function(term) {
        var ret = "";
        for (var i = 0; i < term.length; i++) {
          ret += accentMap[term.charAt(i)] || term.charAt(i);
        }
        return ret;
      };
    
      $("#developer").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));
          }));
        }
      });
    });

提交回复
热议问题