在jquery中自动填充变音符号(色调标记)
问题描述:
我尝试使用jquery-autocomplete
以我的母语(即越南人FWIW)的色调标记。它适用于一个词的精确匹配。但是,我希望搜索功能忽略色调标记,即默认搜索“Lâm”和“Lam”将匹配“Lâm”。在jquery中自动填充变音符号(色调标记)
谢谢。
答
我假设您在客户端数据上使用自动完成功能。
- 对于替换拉丁字母上的变音符号的javascript代码,请参见this answer。
- 在小工具方面,检查
source
option。
这里有一个大纲(未测试)给你的,你如何使用source
功能的想法:
// use the 'removeDiacritics' function from the quoted answer above
function removeDiacritics(str) {
...
}
var myData = ['aäa', 'bbb'];
$('input#autocomplete').autocomplete({
source: function(request, responseCallback){
// 'request' holds the value typed in the input,
// remove diacritics from this value, and build a regexp :
var reSearch = new RegExp(removeDiacritics(request));
// build an array of matched values :
var result = [];
for (var i=0; i<myData.length; i++){
// for each search candidate, run it through removeDiacritics,
// and see if result macthes the (diacritics free) regexp :
if (reSearch.match(removeDiacritics(myData[i]))){
result.push(myData[i]);
}
}
// call the callback with this array :
responseCallback(result);
}
});