Problem with calculations in jquery when multiplying and adding fields

0

Good morning everyone, my problem is this, I have 4 fields

<div class="col-md-12 col-sm-12 col-xs-12 columns" id="multanueva" style="display:none">


<div class="col-md-3 col-sm-12 col-xs-12 row">

    <?php
    echo $form->textFieldGroup($multa_sugerida, 'unidad_tributaria', array(
        'widgetOptions' => array(
            'htmlOptions' => array('class' => 'col-sm-5 numeric span5 c_ut conteo',
                'maxlength' => 5, 'placeholder' => 'Unidades tributarias'))));
    ?>


</div>
<div class="col-md-3 col-sm-12 col-xs-12 row">
    <?php
    echo $form->textFieldGroup($multa_sugerida, 'valorut', array(
        'widgetOptions' => array(
            'htmlOptions' => array('class' => 'col-md-3 numeric ut',
                'maxlength' => 3, 'value' => 177, 'placeholder' => '177 BsF. C/UT', 'readonly' => 'readonly'))));
    ?>
</div>
<div class="col-md-3 col-sm-12 col-xs-12 row">
    <?php
    echo $form->textFieldGroup($multa_sugerida, 'total_unidad_tributaria', array(
        'widgetOptions' => array(
            'htmlOptions' => array('class' => 'col-md-3 sumas',
                'readonly' => 'readonly', 'maxlength' => 40, 'placeholder' => 'Total a pagar (BsF.)'))));
    ?>
</div>
<div class="col-md-3 col-sm-12 col-xs-12 row" >
    <?php
    echo $form->textFieldGroup($multa_sugerida, 'unidad_tributaria', array(
        'widgetOptions' => array(
            'htmlOptions' => array('class' => 'col-md-3 monto','name'=>'multa_nueva',
                'readonly' => 'readonly', 'maxlength' => 40, 'value' => $montoMulta, 'placeholder' => 'Total a pagar (BsF.)'))));
    ?>
</div>

I make a jQuery to multiply the first with the second and the result of this (the third) I add it to the 4th one, that's what I do here

$(".conteo").keyup(function(){
    var Cant =  $(".c_ut").val();
    var ut =  $(".ut").val();
    var multa_vieja =  $(".monto").val();


    multiplicacion = (Cant * ut);  
    $("#MultasSugeridas_total_unidad_tributaria").val(multiplicacion);
           suma = parseFloat(multiplicacion) + parseFloat(multa_vieja);
   // console.log(suma);return false; //
   $(".monto").val(suma);

});

the value of the last field I bring it from database base and I send it from a controller.OK. The detail is that when I do the operations, the last field (the one of the sum and where the result of the same is reflected) adds more than once, and if I delete it continues adding, it is worth to use the change () event, I made operations in different functions, use if statement for when you erase some number so that the 4th field returns to its original value, but NOTHING. But if I use the console.log, if I do the operations properly and even if I delete a number. I really do not know what to do. Thank you very much in advance for the help you can give me, thanks.

    
asked by Juan Romero 07.12.2016 в 16:47
source

1 answer

0
  

The error is surely here:

var multa_vieja =  $(".monto").val();

If you look carefully:

...
var multa_vieja =  $(".monto").val(); // Aquí buscas el valor en 'monto'
...
suma = parseFloat(multiplicacion) + parseFloat(multa_vieja);// Aquí lo sumas
...
$(".monto").val(suma); // Aquí lo asignas el nuevo valor a 'monto'

If you do this again a second time, the previously assigned monto will be added again.

Solution:

var multa_vieja =  $(".sumas").val();
    
answered by 07.12.2016 / 17:55
source