问题描述:

I'm trying to use two autocompletes in a page, connected by each other. I'm trying to pass a variable from one autocomplete to another. I´ll leave you the code to see if someone could help me.

JS:

$(function(){

 $('#buscador').autocomplete({

minLength: 1,

source: "home/get_cliente",

focus: function(event,ui){

$('#buscador').val(ui.item.label);

return false;

},

select: function(event, ui){

id_cliente = ui.item.value;

$('#id_cliente').val(id_cliente);

return false;

$('#vehiculo').autocomplete({

minLength: 1,

source: "home/get_carro?id_cliente="+id_cliente,

this line up here is the one I´m not sure about.

 focus: function(event,ui){

$('#vehiculo').val(ui.item.label);

return false;

}

}); //Fin autocomplete vehiculo

}

}); //Fin autocomplete buscador

});

网友答案:

try this:

     $(function(){
        // Declare global var
        var id_cliente = "";
        $('#buscador').autocomplete({
            minLength: 1,
            source: "home/get_cliente",

            focus: function(event,ui){
                $('#buscador').val(ui.item.label);
                return false;
            },
            select: function(event, ui){
                id_cliente = ui.item.value;
                $('#id_cliente').val(id_cliente);
                return false;  

                    $('#vehiculo').autocomplete({
                    minLength: 1,
                    source: "home/get_carro?id_cliente="+id_cliente, 
                    focus: function(event,ui){
                        $('#vehiculo').val(ui.item.label);
                        return false;
                    }
                }); //Fin autocomplete vehiculo
            }
        }); //Fin autocomplete buscador
    }); 
相关阅读:
Top