4
I am trying to get the value of a table field created with Ajax. With the function below, all fields with the name qtde appear, including what I need. I need to make a simple account of a typed field except that created (qtde - qtde_trans), but it’s complicated.
Function that picks all fields with the name Qtde:
$("input[name^='qtde']").each(function() {
console.log($(this).val());
});
Return (the value I want, in the calculation line, is the 2000):
Whole function I’m testing (with this.val I get the typable field qtde_trans):
function calc_dif() {
if ($(this).val().length > 0) {
var total = $(this).val();
$("input[name^='qtde']").each(function() {
console.log($(this).val());
});
if ($("#qtde").val() < $("#qtde_trans").val()) {
alert("Menor");
}else{
alert(total2);
}
}
}
$(function() {
var $table = $('#locais');
$table.on('change', '.input', calc_dif);
$("#qtde_trans").on('change', calc_dif);
});
How the table is created:
for(var i = 0;i<data.length;i++){
HTML += "<tr><td><input type = 'text' size = '3' name = 'status[]' id = 'status[]' value=" + data[i].status + " readonly></td>";
HTML += "<td><input type = 'text' size = '5' name = 'lote[]' id = 'lote[]' value=" + data[i].lote + " readonly></td>";
HTML += "<td><input type = 'text' size = '10' name = 'endereco[]' id = 'endereco[]' value=" + data[i].endereco + " readonly></td>";
HTML += "<td><input type = 'text' size = '6' name = 'validade[]' id = 'validade[]' value=" + data[i].validade + " readonly></td>";
HTML += "<td><input type = 'text' size = '2' name = 'qtde[]' id = 'qtde[]' value=" + data[i].qtde + " readonly></td>";
HTML += "<td><input type = 'number' style = 'width: 25px;' name = 'qtde_trans[]' id = 'qtde_trans[]' class='input'></td></tr>";
}
Example of the table that loads according to the database data:


I think you should use specifically
$("input[name='qtde[]']"). This should be calculated in one click?– Sergio
That, with the $("input[name='Qtde[]'") that you passed I got what I needed, but when I go to other lines it always displays the first value. That’s something... thank you.!
– Diego
Yeah, that’s why I asked if it’s click-through. But I just saw that you have one
change. It is supposed to recalculate all inputs or only the one of the line that was changed?– Sergio
Only the line that changed. This use change in another function, which I took as an example to start....
– Diego