2017-01-12 4 views
0

Benutzer geben Werte wieund 12345. Ich möchte die 0 ignorieren, wenn ein Benutzer eintritt.jQuery Autocomplete ignorieren 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; 
     } 
    }); 
}); 

Gibt es eine Möglichkeit, das zu tun? Ich habe viele Dinge ausprobiert, aber ich komme damit nicht klar. Irgendeine Idee?

+0

Ja, können Sie die Quellenfunktion verwenden können, request.term enthält Ihren Suchtext. –

+0

Danke Axel. Wie kann ich das machen. Quelle: Funktion (Ereignis, ui) {var zip = parseInt (ui.item.value); ...} – yab86

Antwort

0

Im Folgenden ist nicht getestet, aber es funktioniert in etwa so:

$("#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); 
    } 
});