2017-07-26 57 views
1

我使用選擇二選擇二:修剪輸入空格

<script type="text/javascript"> 
$(document).ready(function() { 
    $(".select2").select2(); 
}); 
</script> 

<select class="select2"> 
    <option>Anna</option> 
    <option>Bob Hunter</option> 
</select> 

我想找到安娜,當我搜索Anna即使有之前或名稱後的空格。如果可能的話,我甚至希望將多個空白總是減少到1,例如Bob Hunter會找到Bob Hunter。是這樣的可能嗎?我在options docs找不到類似的東西。

回答

1

您可以使用下面的代碼開頭&結束脩剪空白

jQuery(element).select2({ 
     matcher: function (params, data) { 
     // If there are no search terms, return all of the data 
     if (jQuery.trim(params.term) === '') { 
      return data; 
     } 
     var myTerm = jQuery.trim(params.term); 
     // `params.term` should be the term that is used for searching 
     // `data.text` is the text that is displayed for the data object 
     if (data.text.toLowerCase().indexOf(myTerm.toLowerCase()) > -1) { 
      // You can return modified objects from here 
      // This includes matching the `children` how you want in nested data sets 
      return data; 
     } 

     // Return `null` if the term should not be displayed 
     return null; 
     } 
    });