jquery - 在输入字段中过滤土耳其语字符

标签 jquery

当我输入普通字符时,如何过滤土耳其语字符? 当我想过滤名称“Gülay Gül”时,当我输入 Gul.. 等时,它不会显示。是否可以使用 jQuery 绕过这个?

var item = $('.item');
  $('input').keyup(function() {
    var valThis = $(this).val().toLowerCase();
    if (valThis == '') {
      $(item).show();
    } else {
      $(item).each(function() {
        var text = $(this).text().toLowerCase();
        var match = text.indexOf(valThis);
        if (match >= 0) {
          $(this).show();
        } else {
          $(this).hide();
        }
      });
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="Search">
<div class="item">
    <h3>John walker</h3>
</div>
<div class="item">
    <h3>Michael Mayer</h3>
</div>
<div class="item">
    <h3>Tim Jones</h3>
</div>
<div class="item">
    <h3>Gülay Gül</h3>
</div>

最佳答案

您正在寻找的称为重音折叠。基于 this这是我的解决方案,更新您的脚本:

var item = $('.item');
  $('input').keyup(function() {
    var valThis = $(this).val().toLowerCase();
    if (valThis == '') {
      $(item).show();
    } else {
      $(item).each(function() {
        var text = accentsTidy($(this).text().toLowerCase());
        var match = text.indexOf(valThis);
        if (match >= 0) {
          $(this).show();
        } else {
          $(this).hide();
        }
      });
    };
  });
  
  accentsTidy = function(s) {
    var map = [
        ["\\s", ""],
        ["[àáâãäå]", "a"],
        ["æ", "ae"],
        ["ç", "c"],
        ["[èéêë]", "e"],
        ["[ìíîï]", "i"],
        ["ñ", "n"],
        ["[òóôõö]", "o"],
        ["œ", "oe"],
        ["[ùúûü]", "u"],
        ["[ýÿ]", "y"],
        ["\\W", ""]
    ];
    for (var i=0; i<map.length; ++i) {
        s = s.replace(new RegExp(map[i][0], "gi"), function(match) {
            if (match.toUpperCase() === match) {
                return map[i][1].toUpperCase();
            } else {
                return map[i][1];
            }
        });
    }
    return s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="Search">
<div class="item">
    <h3>John walker</h3>
</div>
<div class="item">
    <h3>Michael Mayer</h3>
</div>
<div class="item">
    <h3>Tim Jones</h3>
</div>
<div class="item">
    <h3>Gülay Gül</h3>
</div>

关于jquery - 在输入字段中过滤土耳其语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46660923/

相关文章:

jquery - CSS Mega 菜单 - 页面加载时打开一个选项卡 - jQuery?

javascript - 通过js添加HTML元素时阻止IE跳转到页面顶部

javascript - 为什么我的按钮不能禁用 overflow-y :hidden?

javascript - 如何使用jQuery实现 "Hot Keys"

javascript - 如何使用javascript处理jsp页面并插入到主页

javascript - 使用 jquery 防止 iframe 保持焦点

jquery - 如何使用 jQuery 为表格制作下拉列表过滤器?

javascript - 如何将前导 "0"添加到日期字符串的月/日?

javascript - 如何使用 LayerSlider 处理 slider 上的图层?

javascript - Select2 不会在更改/选择第一个选项时触发