How to add amount entered in a dynamically generated input, jquery?

0

the dynamic list is generated in the following way:

var listado_facturas = $('.listado_facturas');
$.each(response.data, function (index, value) {
$('<tr/>')
    .append($('<td/>').addClass('label-cell nuevo-td').text(value.NumeroDocumento))
     /*.append($('<td/>').addClass('label-cell nuevo-td').text(value.Fecha))  */      
    .append($('<td/>').addClass('label-cell nuevo-td').text(value.Cod_Cliente))        
    .append($('<td/>').addClass('label-cell nuevo-td').text(moneda(value.Total)))
    .append('<div class="numero_documento" style="display:none">' + value.NumeroDocumento + '</div>')        
    .append($('<td style=" padding-right: 10px; border-right-width: 10px; padding-left: 10px;"/>').addClass('label-cell classCantidad derecha')
    .append('<input type="text" style="width: 100%; font-size: 12px;" step="0.01" name="cantidad" class="cantidad" attr-num-doc="' + value.NumeroDocumento + '" min="1" pattern="^[0-9]+" placeholder="Monto">'))
    .appendTo(listado_facturas);
})

I am adding the values when entering an input cantidad in the following way:

var total_pago_traslado;
    $('.listado_facturas').on('change', '.cantidad', function(e){
        let  factura = $(this).closest('tr');
        let numero_documento = factura.find('.numero_documento').text();
        let cantidad_ingresada = factura.find('.cantidad').val().replace(/[^0-9\.]+/g,"");


        var dejar = false;
        console.log(parseFloat(factura.find('.cantidad').val()));
        total_pago_traslado += parseFloat(factura.find('.cantidad').val());
        console.log(total_pago_traslado);
});

but the result in line console.log(parseFloat(factura.find('.cantidad').val())); if it is numeric for example (10), but in line console.log(total_pago_traslado); returns NaN.

Thank you in advance.

    
asked by JG_GJ 21.11.2018 в 05:55
source

2 answers

2

You have the error in the declaration of variable var total_pago_traslado; .

If you do not start the variable (in 0 for example), you can not use the operation of += since you are adding a value to another value NaN .

The solution is as simple as starting the variable, for example:

var total_pago_traslado = 0;
    
answered by 21.11.2018 / 08:11
source
0

That code will add the value of the field each time you change it . You should add all the values every time:

var total_pago_traslado;

var sum_cancidades = function () {
    var total = 0;
    // puedes pasar un argumento a la función para filtrar
    // por una factura específica en vez de '.listado_facturas'
    $('.listado_facturas .cantidad').each( function () {
        // puedes limpiar el valor del campo aquí si quieres
        total += parseFloat($(this).val());
    } );
    console.log('total:', total);

};

total_pago_traslado = sum_cancidades();

$(document).on('change', '.listado_facturas .cantidad', function(e) {
    total_pago_traslado = sum_cancidades();
});
    
answered by 21.11.2018 в 10:23