Problem with calculating the total value Javascript

Asked

Viewed 49 times

0

I’m having trouble recalculating the value of the OTHERS field with the TOTAL value. I need to add the value placed in the other field the first time, if I change the precise value recalculate and add the difference: For example the total value is 29.00 if I put other value 10.00 is 39.00 but if I change to 20.00 calculate the difference and get the result 49.00. Currently my problem is that it is always increasing when I change the value of the other field and adding in total, if I put 10,00 and change the focus, calculates correct, but if I change to 20,00 adds 10,00 + 20,00.

Following is illustration and code image: inserir a descrição da imagem aqui

let total  = 0;
$('#valout').on('change', function(){
    let valout = $(this).val();
    let subtot = replaceNumber($('#totalpedido').val());

    if(isNaN(valout)) valout = 0;
    if(isNaN(subtot)) subtot = 0;

    if(valout > 0) {
        $('#valout').data('val-default', valout);
        total   = subtot + valout;
    } else {
        let def = $('#valout').data('val-default');
        total   = subtot - def;
    }

    $('#totalpedido').val(replaceDecimal(total));
});
  • In my opinion you should run the calculation in a separate function, isolate the calculation behavior and after certain action you perform this function, so both in the output of Focus and in the change would always have the same behavior of executing the same function, who would always be doing the calculation from the start.

  • 1

    Try to explain this problem better, divide it into items, I was very confused when reading.

  • Check that this reply help you. As @Felippetadeu commented, you’d better separate the function from the calculation and call as the events you need.

  • I edited here I put a gif, notice the total value

  • But your gif is so small and blurry that you can’t even see anything.

1 answer

0

The problem is that you have declared the variable total out of the function. So it will always add new values when calling the event change. The correct is to declare it within the function to always start with the value 0 to make new calculations:

$('#valout').on('change', function(){

    let total  = 0; // DECLARE AQUI

    let valout = $(this).val();
    let subtot = replaceNumber($('#totalpedido').val());

    if(isNaN(valout)) valout = 0;
    if(isNaN(subtot)) subtot = 0;

    if(valout > 0) {
        $('#valout').data('val-default', valout);
        total   = subtot + valout;
    } else {
        let def = $('#valout').data('val-default');
        total   = subtot - def;
    }

    $('#totalpedido').val(replaceDecimal(total));
});

Or if you want it global, declare outside the function but change the value to 0 within the function:

let total; // DECLARE AQUI
$('#valout').on('change', function(){

    total = 0; // Altera o valor

    let valout = $(this).val();
    let subtot = replaceNumber($('#totalpedido').val());

    if(isNaN(valout)) valout = 0;
    if(isNaN(subtot)) subtot = 0;

    if(valout > 0) {
        $('#valout').data('val-default', valout);
        total   = subtot + valout;
    } else {
        let def = $('#valout').data('val-default');
        total   = subtot - def;
    }

    $('#totalpedido').val(replaceDecimal(total));
});
  • I haven’t been able to solve it yet, I tried to do it that way but I didn’t succeed in this Link shows the problem better, whenever I put value in the field others it must reset and recalculate the current value instead of incrementing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.