Calculation problems with jquery.maskMoney

Asked

Viewed 924 times

1

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

1- COST, RECEIVES THE COST OF A PRODUCT 2- SALE, receives p selling price, and automatically calculates profit margin 3- %PROFIT, receives the profit and automatically calculates the selling price.

The problem is that I am trying to use the plugin jquery.maskMoney in the PROFIT input, and when I use it the calculation of the sales price does not happen. Someone knows what 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');
});

Just follow 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>

<script src="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>

1 answer

1


Hello,

To solve your problem, change the event you are calling in ". margin" to keyup instead of input.

In your code it’s like this:

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

Change to:

$(".margem").on("keyup", function() {
  • VERY GOOD, IT WORKED 100%

Browser other questions tagged

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