Well, I need to apply the $(".margem").keyup();
event only to the input that contains a value greater than 0.
Notice that by changing the cost value the system applies the $(".margem").keyup();
event by updating the input valor
, but the second value input does not contain a margin and therefore can not be updated.
Does anyone know how to do this verification?
$(document).ready(function() {
// Verifica se o custo mudou
$(".custo").on("keyup", function() {
$(".margem").keyup();
});
// Faz o calculo do valor com base na margem
$(".margem").on("keyup", function() {
// Margem
var margem = $(this).val();
var custo = $('#custo').val();
// Formata valor para pt-br
calculo = margem + custo;
// Atualiza input
var inputValor = $(this).attr("valor");
$("#" + inputValor).val(calculo).trigger('blur');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
custo:
<input type='text' class='custo' id='custo' name='custo' value='8'>
<br>
<br>
<br> Valor X Margem<br>
<input type='text' class='valor valores' id='valor1' name='valor1' margem='margem1'>
<input type='text' class='margem lucro' id='margem1' name='margem1' valor='valor1' value='1'>
<br>
<br>
<br> Valor X Margem<br>
<input type='text' class='valor valores' id='valor2' name='valor2' margem='margem2'>
<input type='text' class='margem lucro' id='margem2' name='margem2' valor='valor2'>