Search MySql using a drop-down list select with php Ajax Jquery

0

I am doing a search input using the event keyup in jquery, now if instead of input I use a select what event would I use or how would I do in that case?

This is the input

<input type="text" name="pedido" class="form-control" id="pedido" required>

and this is the new select

<select name="pedido" id="busquedas" class="form-control" required>
<option> </option>
<option>FD1</option>
<option>FD2</option>
<option>FD3</option>
</select>

This is the jquery

    $(obtener_registros());
function obtener_registros(pedido)
{
    $.ajax({
        url : 'ajax/ajax-consulta.php',
        type : 'POST',
        dataType : 'html',
        data : { pedido: pedido },
        })
    .done(function(resultado){
        $("#datos_cliente").html(resultado);
    });
}
$(document).on('keyup', '#busquedas', function(){
    var valorBusqueda=$(this).val();
    if (valorBusqueda!="")
    {
        obtener_registros(valorBusqueda);
    }
    else
        {
            obtener_registros();
        }
});
    
asked by Daniel 03.08.2017 в 16:47
source

1 answer

3

The event you have to capture is change .

$(document).on('change', '#busquedas', function(){
    var valorBusqueda=$(this).val();
    if (valorBusqueda!="")
    {
        obtener_registros(valorBusqueda);
    }
    else
    {
        obtener_registros();
    }
});

And with this you make sure that $(this).val() has the value that the user has selected.

You can see more event information change in the official JQuery documentation.

    
answered by 03.08.2017 / 16:53
source