3
Well I edited the question, because I located the problem. I have a javascript responsible for making calculations with the values in the input, and I have a plugin Jquery Mask or Maskmoney to make the formatting of the entered value.
Both plugins have the same error.
Jquery Mask - When I type 1,00 the javascript that makes the calculation takes the value 100.
Maskmoney - When I type something, the javascript that makes the calculation thinks the field is empty.
I need to help, I thank you already
Follows the code:
$(document).ready(function() {
$(".valor").on("input", function() {
// Margem
var valor = $(this).val();
valor = valor.replace(".", "");
valor = valor.replace(",", ".");
// Custo
var valorCusto = $("#custo").val();
valorCusto = valorCusto.replace(".", "");
valorCusto = valorCusto.replace(",", ".");
var calculo = (parseFloat(valor) - parseFloat(valorCusto)) / parseFloat(valorCusto) * 100;
var inputMargem = $(this).attr("margem");
var resultadoMonetario = calculo.toFixed(2).replace(".", ",");
$("#" + inputMargem).val(resultadoMonetario);
});
$(".margem").on("input", function() {
// Margem
var valorMargem = $(this).val();
valorMargem = valorMargem.replace(".", "");
valorMargem = valorMargem.replace(",", ".");
// Custo
var valorCusto = $("#custo").val();
valorCusto = valorCusto.replace(".", "");
valorCusto = valorCusto.replace(",", ".");
var calculo = (parseFloat(valorCusto) * parseFloat(valorMargem) / 100) + parseFloat(valorCusto);
var inputValor = $(this).attr("valor");
var resultadoMonetario = calculo.toFixed(2).replace(".", ",");
$("#" + inputValor).val(resultadoMonetario);
});
});
$('.valores').mask('00.000.000,00', {
reverse: true
});
<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.mask/1.13.4/jquery.mask.js"></script>
Custo:
<input type='text' id='custo' class='custo valores' name='custo'>
<br>
<br> Valor:
<input type='text' id='valor1' class='valor valores' name='valor1' margem='margem1'> Margem:
<input type='text' id='margem1' class='margem valores' name='margem1' valor="valor1">
<br> Valor:
<input type='text' id='valor2' class='valor valores' name='valor2' margem='margem2'> Margem:
<input type='text' id='margem2' class='margem valores' name='margem2' valor="valor2">
the strange and that here in the stack it works. Example:
1st cost = 5,55 2nd margin = 1,00 Result = 5,61
Already the same problem code on my page and here:
Hugo, not to be a dick to you, but if you used Mask-money you wouldn’t have any of these problems you’re encountering. As I told you in another answer, working with values is much more than recommended, including by the jQuery Mask community. I’m sorry to insist, but from my point of view, you’re wasting time on something you wouldn’t need.
– Pedro Camara Junior
This conversion you need to do, the plugin itself has by default, as I answered in your other question
– Pedro Camara Junior
Pedro I tried to use with maskmoney and this giving problem, look at https://jsfiddle.net/7aq46h0x/12/ I realized that with maskMoney when I type a value and as if the input was empty, and for Mask and as if it had no commas. The problem is this
– Hugo Borges
https://jsfiddle.net/7aq46h0x/18/
– Pedro Camara Junior
Nice, but the only problem and that needs to go out of input to do the calculation, has how to change this to the calculation to be done in real time?
– Hugo Borges
Dude, I already gave you a ready answer, which is not the point of Stackoverflow. How about trying to understand what you’re doing? Take a look at this one link. Excuse the rudeness, but a minimum of effort is valid, do not think?
– Pedro Camara Junior
yes I fully agree
– Hugo Borges