Problems with calculations with jquery.maskMoney

1

I have a javascript that makes the perfect calculation between 3 inputs. Being them.

1- COST, you get the cost of a product 2- SALE, receive the sales price, and automatically calculate the profit margin 3% PROFIT, receive the profit and automatically calculate the sales price.

The problem is that I am trying to use the jquery.maskMoney plugin in the PROFIT input, and when I use it, the sales price calculation does not happen. Does anyone know what it can be?

To see the code working perfectly just delete the following code.

$(function() {
    $(".margens2").maskMoney({
        allowNegative: true,
        thousands: '.',
        decimal: ',',
        affixesStay: false
    }).trigger('mask.maskMoney');
});

Here is my code:

$(function() {
        $(".margens").maskMoney({
            allowNegative: true,
            thousands: '.',
            decimal: ',',
            affixesStay: false
        }).trigger('mask.maskMoney');
    });

$(document).ready(function() {

  // Faz o calculo da margem com base no valor
  $(".valor").on("input", function() {
    // Margem
    var valor = $(this).val();
    var valorCorrigido = parseFloat(adicionarPontos(valor));
    var valorFloat = parseFloat(valorCorrigido) || 0.0;

    // Custo  
    var valorCusto = $('#custo').val();
    var valorCustoCorrigido = parseFloat(removerPontos(valorCusto));
    var valorCustoFloat = parseFloat(valorCustoCorrigido) || 0.0;

    // Calculo
    var calculo = (parseFloat(valorFloat) - parseFloat(valorCustoFloat)) / parseFloat(valorCustoFloat) * 100;
    var inputMargem = $(this).attr("margem");

    var resultadoMonetario = calculo.toFixed(2).replace(".", ",");

    $("#" + inputMargem).val(resultadoMonetario).trigger('blur');
  });

  // Faz o calculo do valor com base na margem
  $(".margem").on("input", function() {
    // Margem
    var valorMargem = $(this).val();
    var valorMargemCorrigido = parseFloat(adicionarPontos(valorMargem));
    var valorMargemFloat = parseFloat(valorMargemCorrigido) || 0.0;

    // Custo
    var valorCusto = $('#custo').val();
    var valorCustoCorrigido = parseFloat(removerPontos(valorCusto));
    var valorCustoFloat = parseFloat(valorCustoCorrigido) || 0.0;

    // Cálculo
    var calculo = (parseFloat(valorCustoFloat) * parseFloat(valorMargemFloat) / 100) + parseFloat(valorCustoFloat);
    var inputValor = $(this).attr("valor");

    var resultadoMonetario = calculo.toFixed(2).replace(".", ",");

    $("#" + inputValor).val(resultadoMonetario).trigger('blur');
  });

  function removerPontos(valor) {
    valor = valor.replace(".", "");
    valor = valor.replace(",", ".");
    return valor;
  }

  function adicionarPontos(valor) {
    if (valor.length === 1) {
      return valor;
    } else {
      if (valor.length === 2) {
        return valor;
      } else {
        valor = valor.replace(",", "");

        var inteiro = valor.substring(0, valor.length - 2);
        var decimal = valor.substring(valor.length - 2, valor.length);

        return inteiro + "." + decimal;
      }
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>



<div class='form-group'>
  <label class='control-label' for='custo'>CUSTO*</label>
  <input type='text' class='form_campos form_campos_simples custo' id='custo' name='custo' value="10">
</div>
<div class='form-group'>
  <label class='control-label' for='valor1'>VENDA</label>
  <input type='text' class='form_campos form_campos_numeros valor valores' id='valor1' name='valor1' margem='margem1'>
</div>
<div class='form-group'>
  <label class='control-label' for='margem1'>% LUCRO</label>
  <input type='text' class='form_campos form_campos_numeros margem margens' id='margem1' name='margem1' valor='valor1'>
</div>
    
asked by anonymous 15.07.2016 / 22:00

1 answer

1

Hello,

To resolve your issue, change the event you are calling in ".marge" to keyup instead of input .

In your code it looks like this:

$(".margem").on("input", function() {

Switch to:

$(".margem").on("keyup", function() {
    
20.07.2016 / 00:47