2
As the example below, I am trying to add the value of the lines Qtde Transferir
, that are generated dynamically as the result of the database, subtract from the value entered in the field Quantidade a Transferir
and display the difference in the field Falta
:
I built the code below, using some examples I found, he added the dynamic lines, but first I need to type the values in the lines and finally the Quantidade a Transferir
to calculate the Falta
:
$(document).ready(function() {
$("#qtde_entrada").on('change', function() {
if($(this).val().length > 0) {
var total = 0;
$('.input').each(function(){
var valor = Number($(this).val());
if (!isNaN(valor)) total += valor;
});
var final = $("#qtde_entrada").val() - total;
$("#qtde_falta").val(final);
}
});
});
Inputs are set this way:
Qtde to Transfer and Missing:
<tr>
<td colspan='2'>Quantidade a Transferir:
<input type = "number" id="qtde_entrada" name = "qtde_entrada" min="1" max="99999" style="width: 10em;"> -
<b>Falta: <input type = "number" id="qtde_falta" name = "qtde_falta" class="resultado" style="width: 10em;" readonly></b></td>
</tr>
Qtde Transfer (dynamic):
HTML += "<td><input type = 'text' size = '5' name = 'qtde_trans[]' id = 'qtde_trans[]' class='input' ></td></tr>";
Any suggestion of how to first type the total value to transfer and as it is typed in the lines it will display in the missing?
Thanks for the reply. I’m looking at Jsfiddle and it really worked. In my case not yet, but I’m reviewing again.
– Diego
@Diego Como applied the modifications?
– rdleal
It worked, I missed a # in the part I changed in var table. Thank you for the excellent explanation and reply. Thank you very much.
– Diego