2016-12-03 26 views
3

我用select2.js在萬畝網站我的網站。外語是波斯語。我填寫選擇元素使用此選項爲什麼Select2框架找不到一些Unicode字符?

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

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(); 

當我在選擇2搜索框中鍵入

ك

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(); 
相關問題