【问题标题】:Auto-complete with diacritics (tonal marks) in jquery在 jquery 中使用变音符号(音调标记)自动完成
【发布时间】:2013-04-10 10:38:14
【问题描述】:

我正在尝试在我的母语(越南语 FWIW)中使用带有音调标记的 jquery-autocomplete。它对单词的精确匹配很有效。但是,我希望搜索功能忽略音调标记,即默认情况下搜索“Lâm”和“Lam”将匹配“Lâm”。

谢谢。

【问题讨论】:

    标签: jquery jquery-autocomplete


    【解决方案1】:

    我假设您对客户端数据使用自动完成功能。

    • 有关替换拉丁字母变音符号的 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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2012-02-18
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 2011-12-15
      • 2023-03-27
      相关资源
      最近更新 更多