jQuery自动完成忽略0

时间:2017-01-12 12:30:18

标签: php jquery html autocomplete

用户输入01234和12345等值。我想在用户输入时忽略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;
        }
    });
});

有没有办法做到这一点?我尝试了很多东西,但我无法处理它。有什么想法吗?

1 个答案:

答案 0 :(得分: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);
    }
});