2017-10-10 28 views
4

如何在输入普通字符时过滤土耳其字符? 当我想要过滤名称'GülayGü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>

+1

我不能帮你,但我确实知道了jQuery自动完成构件具有该功能默认情况下建立,https://jqueryui.com/autocomplete/#folding – Vincent1989

回答

6

什么你正在寻找基于this这里被称为accent folding.是我的解决方案,更新你的脚本:

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>

1

随着更换Turkish CHARAC用English字符你可以绕过这个。使用regex过滤土耳其语单词并用英语替换它们,实际上它在比较之前替换文本。

// search filter 
 
    var item = $('.item'); 
 
    var Turkish = { 
 
     Ç: 'C', 
 
     Ö: 'O', 
 
     Ş: 'S', 
 
     İ: 'I', 
 
     I: 'i', 
 
     Ü: 'U', 
 
     Ğ: 'G', 
 
     ç: 'c', 
 
     ö: 'o', 
 
     ş: 's', 
 
     ı: 'i', 
 
     ü: 'u', 
 
     ğ: 'g' 
 
    }; 
 

 

 
    $('input').keyup(function() { 
 
    var valThis = $(this).val().toLowerCase(); 
 
    if (valThis == '') { 
 
     $(item).show(); 
 
    } else { 
 
     $(item).each(function() { 
 
     var text = $(this).text().toLowerCase(); 
 
     
 
var clearTr = text.replace(/Ü|Ç|Ö|Ş|İ|I|Ğ|ç|ö|ş|ı|ü|ğ/gi, function(matched){ 
 
    return Turkish[matched]; 
 
}); 
 
     var match = clearTr.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> 
 
<div class="item"> 
 
    <h3>Derya Uluğ</h3> 
 
</div>

+0

它为我回来三场比赛。 @pedram – Optional

+0

@可选,是的,现在试图找出问题所在。 – Pedram