2016-12-03 30 views
3

私はペルシャ.Iは、このオプションを使用してselect要素を満たしているミューウェブサイト私のウェブサイトの.LanguageにSelect2フレームワークでユニコード文字が見つからないのはなぜですか?

<select name="TrainDeparture" class="full-width select2" id="TrainDeparture"> 
    <option value="165">كرج</option> 
</select> 

JSをselect2.jsを使用します。

(function ($) { 
    "use strict"; 

    $.fn.select2.locales['fa'] = { 
     formatMatches: function (matches) { return matches + " نتیجه موجود است، کلیدهای جهت بالا و پایین را برای گشتن استفاده کنید."; }, 
     formatNoMatches: function() { return "نتیجه‌ای یافت نشد."; }, 
     formatInputTooShort: function (input, min) { var n = min - input.length; return "لطفاً " + n + " نویسه بیشتر وارد نمایید"; }, 
     formatInputTooLong: function (input, max) { var n = input.length - max; return "لطفاً " + n + " نویسه را حذف کنید."; }, 
     formatSelectionTooBig: function (limit) { return "شما فقط می‌توانید " + limit + " مورد را انتخاب کنید"; }, 
     formatLoadMore: function (pageNumber) { return "در حال بارگیری موارد بیشتر…"; }, 
     formatSearching: function() { return "در حال جستجو…"; } 
    }; 

    $.extend($.fn.select2.defaults, $.fn.select2.locales['fa']); 
})(jQuery); 
$('.select2').select2(); 

私はSELECT2の検索ボックスに入力

ك

charactそれはの結果が見つかりませんでしたメッセージを表示しますしかしこれは間違っています。

キーボードにアラビア語を追加して再テストします。この場合、現在select2 が見つかりました。今、どうすれば修正できますか?

jsbin link

+1

あなたのページエンコードは何ですか? JavaScript(select2ライブラリで使用される)はutf8/utf16のみを認識します。ページのエンコーディングはそれに準拠している必要があります(ページの 'head'セクション参照)。 'select' html要素からデータがJSに来るので、データのソースはHTML =>エンコーディングが重要です – smnbbrv

+0

私はヘッドセクションでを使用します – programmer138200

+0

あなたはutf16と一緒に行くことができますか? – smnbbrv

答えて

0

私は同様の問題がありました。私はカスタムマッチャーを作った。

function customMatcher (params, data){ 
    // Always return the object if there is nothing to compare 
    if ($.trim(params.term) === '') { 
     return data; 
    } 

    if (data.children && data.children.length > 0) { 
     // Clone the data object if there are children 
     // This is required as we modify the object to remove any non-matches 
     var match = $.extend(true, {}, data); 

     // Check each child of the option 
     for (var c = data.children.length - 1; c >= 0; c--) { 
      var child = data.children[c]; 

      var matches = customMatcher(params, child); 
      // console.log(matches); 

      // If there wasn't a match, remove the object in the array 
      if (matches == null) { 
       match.children.splice(c, 1); 
      } 
     } 

     // If any children matched, return the new object 
     if (match.children.length > 0) { 
      return match; 
     } 

     // If there were no matching children, check just the plain object 
     return customMatcher(params, match); 
    } 

    var original = data.text.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toUpperCase(); 
    var term = params.term.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toUpperCase(); 

    // Check if the text contains the term 
    if (original.indexOf(term) > -1) { 
     return data; 
    } 

    // If it doesn't contain the term, don't return anything 
    return null; 
} 
$('select').select2({ 
    matcher: customMatcher 
}); 

元のものとの主な違いは、どのように発音区別記号が削除されるかです。

var original = data.text.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toUpperCase(); 
var term = params.term.normalize('NFD').replace(/[\u0300-\u036f]/g, "").toUpperCase(); 
関連する問題