1
Well, I need to apply the event $(".margem").keyup();
only for input containing a value greater than 0.
Note that when changing the cost value the system applies the event $(".margem").keyup();
updating the input valor
, but the second input value does not contain a margin and so it cannot be updated.
Does anyone know how to do this check?
$(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'>
In fact, you don’t need to check that the value is greater than zero in Javascript. Changes the "type" of your input to "number" and adds the property
min = '1'
no. But if you really want to use code to verify, use the function @Mathias wrote– Gabriel Polidoro