2017-01-12 94 views
0

用戶輸入值如和12345.我想在用戶輸入時忽略0。jQuery自動完成忽略0

<input name="location" class="input-lg" style="width:100%;" id="location" type="text" size="50" placeholder="<?php echo $this->lang->line('inputhint');?>" /> 

$(function() { 
    var availableLocations = 
    <?php 
     // print the json array containing all zips 
     echo $locations; 
    ?> 

    $("#location").autocomplete({ 
     source: availableLocations, 
     minLength: 3, 
     focus: function (event, ui){ 
      $('#location').val(ui.item.value); 
      return false; 
     }, 
     select: function (event, ui){ 
      $('#location').val(ui.item.value); // display the selected text 
      $('#locationid').val(ui.item.key); // save selected id to hidden input 
      return false; 

     }, 
     change: function (event, ui){ 
      // write value to hidden field 
      $("#locationid").val(ui.item? ui.item.key : 0); 
      return false; 
     } 
    }); 
}); 

有沒有辦法做到這一點?我嘗試了很多東西,但我無法處理它。任何想法?

+0

是的,你可以使用源功能,request.term包含搜索字符串。 –

+0

謝謝阿克塞爾。我怎樣才能做到這一點。 source:function(event,ui){var zip = parseInt(ui.item.value); ...} – yab86

回答

0

以下是未經測試,但它的工作原理是這樣的:

$("#location").autocomplete({ 
    source: function(request, response) { 
     // manipulate your search term 
     var yourTerm = parseInt(request.term); 
     // run a filter function on your data... 
     var filteredArray = $.map(availableLocations, function(item) { 
      // or whatever your matching rule is.... 
      if(item.startsWith(yourTerm)){ 
       return item; 
      } else{ 
       return null; 
      } 
     }); 
     // return the filtered values 
     response(filteredArray); 
    } 
});