简体   繁体   中英

Dynamic Add Input - Total Price after Update Item Quantity

I need to update automatically the Total amount after changing the quantity of an item.

I checked this question and also this question, among others but, unfortunately I can't figure out how to get the quantity value of the row and update the Total.

I have this code -> https://jsfiddle.net/ricardofranco/gaf32kry/30/

My JS

$('#add_row').click(function() {

    $("#linha").first().clone(true).appendTo("tbody").append("<td data-name='del'><button name='' class='btn btn-danger glyphicon glyphicon-remove row-remove'><span aria-hidden='true' id='row-remove'>-</span></button></td>");

});

$("#linha").on('click', '.row-remove' ,function() {
    $(this).closest("#linha").remove();
});


//sum all values
$('.table-primary').on('input', '.value', function(){

    var totalSum = 0;

    $('.table-primary .value').each(function(){
        var inputValue = $(this).val();

        if(inputValue){

            totalSum += parseFloat(inputValue);
        }

    });

    $('#total').text(totalSum).innerHTML;

});

Can you guys help me on this?

Update the code that sums the amount to multiply the Quantity field.

$('.table-primary').on('keyup', '.value', function(){
var totalSum = 0;

$('.table-primary .value').each(function(){
    var inputValue = $(this).maskMoney('unmasked')[0];
    // Get quantity from same row (tr)
    var quantity = $(this).closest('tr').find('.qtd').val() || 0;

    if(inputValue){
        totalSum += (parseFloat(inputValue) * quantity);
    }
});

$('#total').text(totalSum.toFixed(2)).innerHTML;

});

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