How to get the value of several dynamically generated input, Jquery?

0

Good day. I generate a list of products in a table dynamically in the following way:

var productos;
    $$("#productos").blur(function(e){
        console.log("Mostrar resultado del producto");
        var CodigoInventario = 0016;

        //--- Buscar productos ---
        axios.get('https://URL/AppBuscarProducto?IDPedido=${idPedido}&CodigoInventario=${CodigoInventario}',{
                headers: {
                    Authorization: token_type + " " + access_token
                }
             }).then(function (response) {
                console.log(response);
                productos = response.data;
                $("#resultadoProductos > tbody > tr").remove();
                var resultadoProductos = $('#resultadoProductos');
                $.each(productos, function(i){
                    $('<tr/>')
                            .append($('<td/>').addClass('label-cell').append(productos[i].Descripcion))
                            .append($('<td/>').addClass('label-cell').append(productos[i].PrecioSocio))
                            .append('<input id="descripcionProducto" value="'+productos[i].Descripcion+'" style="display:none"></input>')
                            .append($('<td/>').addClass('label-cell').append('<button value="'+productos[i].Codigo_Inventario+'" type="button" id="btn-agregarCarrito" class="button button-fill button-raised button-circle color-green" title="Comment"><i class="fa fa-shopping-cart"></i></button>'))
                    .appendTo(resultadoProductos);
                });
            }).catch(function (error) {
                console.log(error);
                myApp.hideIndicator();
            });

    });

and in the next function I capture the value of the data generated in the table dynamically, for now I have only been able to capture the value of the button.

var valCarrito = [];
$$("#resultadoProductos").click('#btn-agregarCarrito',function(e){
    var codProducto  = $(this).val(); //Aqui capturo el valor del boton btn-agregarcarrito, este si muestra los valores dinamicos, es decir no muestra siempre el mismo valor del boton, sino va cambiando segun lo que haya seleccionado.
    var descProducto = $("#descripcionProducto").val(); //aca debo obtener el valor del input descripcionProducto, pero unicamente logro obtener el primer valor generado, 
    var cantProducto = $("#cantProducto").val();

    console.log("SE AGREGA UN PRODUCTO");
    console.log(codProducto);
    console.log(descProducto);
    valCarrito.push({
        "codProducto": codProducto,
        "cantProducto": cantProducto
    })

    console.log("DATOS DEL CARRITO:");
    console.log(valCarrito);
    e.preventDefault();
});
    
asked by JG_GJ 31.08.2018 в 20:38
source

1 answer

0

The correction to the problem is as follows:

    var valCarrito = [];
$$("#resultadoProductos").click('#btn-agregarCarrito',function(e){
    var codProducto  = $(this).val();
    var descProducto = $(this).parents("tr").find(".descripcionProducto").val();//De esta forma se obtiene el dato.

    console.log("SE AGREGA UN PRODUCTO");
    console.log(codProducto);
    console.log(descProducto);
    valCarrito.push({
        "codProducto": codProducto,
        "cantProducto": cantProducto
    })

    console.log("DATOS DEL CARRITO:");
    console.log(valCarrito);
    e.preventDefault();
});
    
answered by 11.09.2018 / 17:58
source