提前输入不敏感的重音



我尝试了这个解决方案,但我得到了这个错误:

Uncaught ReferenceError: normalized is not defined

下面是我的代码:
var charMap = {
    "à": "a", "â": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
    "ï": "i", "î": "i", "ô": "o", "ö": "o", "û": "u", "ù": "u"
};
var normalize = function(str) {
      $.each(charMap, function(chars, normalized) {
        var regex = new RegExp('[' + chars + ']', 'gi');
        str = str.replace(regex, normalized);    
      });
      return normalized;
    }
var queryTokenizer = function(q) {
    var normalized = normalize(q);
    return Bloodhound.tokenizers.whitespace(normalized);
};
var spectacles = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: queryTokenizer,
    prefetch:'spectacles.json',
    limit:10,
  });
spectacles.initialize();
$('#search').typeahead({
  minLength: 1,
  hint:false,
  highlight: true
}, 
 {
  name: 'spectacles',
  displayKey: 'value',
  source: spectacles.ttAdapter()
}) ;

我的错误在哪里?由于

如果你不想使用Bloodhound,你可以从Typeahead对象中定制'highlighter'和'matcher'方法,使它们变得不敏感。

如果你想让重音不敏感成为预输入的默认行为,你可以像下面这样包含一个新的JavaScript文件:

步骤1 -创建一个新文件bootstrap3-typeahead-ci.min.js

// function for making a string accent insensitive
$.fn.typeahead.Constructor.prototype.normalize = function (str) {
    // escape chars
    var normalized = str.replace(/[-[]/{}()*+?.\^$|]/g, "\$&");
    // map equivalent chars
    normalized = normalized.replace(/[aãáàâ]/gi, '[aãáàâ]');
    normalized = normalized.replace(/[eẽéèê]/gi, '[eẽéèê]');
    normalized = normalized.replace(/[iĩíìî]/gi, '[iĩíìî]');
    normalized = normalized.replace(/[oõóòô]/gi, '[oõóòô]');
    normalized = normalized.replace(/[uũúùû]/gi, '[uũúùû]');
    normalized = normalized.replace(/[cç]/gi, '[cç]');
    // convert string to a regular expression
    // with case insensitive mode
    normalized = new RegExp(normalized, 'gi');
    // return regular expresion
    return normalized;
}
// change 'matcher' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.matcher = function (item) {
    // get item to be evaluated
    var source = this.displayText(item);
    // make search value case insensitive
    var normalized = this.normalize(this.query);
    // search for normalized value
    return source.match(normalized);
}
// change 'highlighter' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.highlighter = function (item) {
    // get html output
    var source = this.displayText(item);
    // make search value case insensitive
    var normalized = this.normalize(this.query);
    // highlight normalized value in bold
    return source.replace(normalized, '<strong>$&</strong>');
}

步骤2 -添加到你的页面后bootstrap3-typeahead.min.js

<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>

当然,你必须意识到,既然你要替换这两个方法,你应该监视typeahead中发布的新特性是否不会反映在你的自定义方法中。然而,我认为这是一个轻量级和快速的解决方案。

p。这是我对Stack Overflow的第一次贡献,希望你喜欢!

更改normalize函数,使其返回规范化字符串,即

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

看看我写的这段代码:

http://jsfiddle.net/Fresh/SL36H/

您可以在浏览器调试控制台中看到规范化字符串。在我的示例中,"àààèèèùùù"被转换为"aaaeeeuuu"。

请注意,我已经改变了函数参数,使它们更准确(即字符是不正确的,它应该是char),我也合理化了正则表达式。

可以使用String.prototype.normalize()

https://developer.mozilla.org/en-(美国/docs/Web/JavaScript/引用/Global_Objects/字符串/正常化][1]

var queryTokenizer = function(input) {
        input = input.normalize("NFD").replace(/[u0300-u036f]/g, "")
        return Bloodhound.tokenizers.whitespace(input);
};

相关内容

  • 没有找到相关文章

最新更新