简体   繁体   中英

Match user's typed input with jquery autocomplete

I'm using jquery-ui autocomplete to validate some form inputs. I've got this code:

function addAutoComplete(beacon, url) {
    $(document).ready(function(){
        $('input' + beacon).autocomplete({
            source: url,
            minLength:3,
            select: function(event, ui) {
                var selectedObj = ui.item;
                $(beacon+'_autocomplete_id').val(selectedObj.id);
                return false;
            },
            change: function(event, ui){
                if (ui.item == null){
                           $(beacon+'_autocomplete_id').val(0);
                } else {
                   $(beacon+'_autocomplete_id').val(ui.item.id);
                   }

            }
            });
        });
}       

The goal is simple: associate the "id" value of the datas with an hidden field. When the user's input is not in the source, we put a "0". My problem is that when the user types everything and do not click or make use of the autocomplete, he could write a proper value but it would not be acknowleged as such.

I've seen how to avoid this problem if you store the source in a local array, for instance, but is there a way to do this with the source coming from the url? In other terms, is there a property that allow me to manipulate the JSON I go from the url without having to code my callback function for the source?

Thanks in advance !

Found a solution: using Scott González wonderful autoSelect option . Works perfectly, really great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM