Talk to everyone, baby, with you? I'm tinkering with my system (read laboratory).
I've basically created the whole Back-End, but on the front I'm getting some treatments.
I am creating a budgets screen, where the User is dynamically opening new lines for each product added.
My code looks like this:
function id(valor_campo) {
return document.getElementById(valor_campo);
}
function getValor(valor_campo) {
var valor = document.getElementById(valor_campo).value.replace(',', '.');
return parseFloat(valor);
}
function multiplica() {
var total = getValor('qtd') * getValor('valoru');
id('valort').value = total;
}
$(document).ready(function() {
var max_fields = 100;
var x = 1;
$('#add_field').click(function(e) {
e.preventDefault(); //prevenir novos clicks
if (x < max_fields) {
$('#listas').append('\
<tbody>\
<tr class="remove' + x + '">\
<td><input type="text" name="qtd[]" id="qtd[]" placeholder="N°"></td>\
<td><input type="text" name="item[]" id="item" placeholder="Descrição do Produto ou Serviço"></td>\
<td><input type="text" name="valoru[]" onblur="multiplica()" id="valoru[]" placeholder="Valor"></td>\
<td><input type="text" name="valort[]" id="valort[]" maxlength="100" /></td>\
</td>\
<td><a href="#" class="remove_campo" id="remove' + x + '"><button type="button" class="btn-danger">Remover</button></a></td>\
</tbody>\
');
x++;
}
});
$('#listas').on("click", ".remove_campo", function(e) {
e.preventDefault();
var tr = $(this).attr('id');
$('#listas tr.' + tr).remove();
x--;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableid="listas" class="table table-hover">
<thead>
<tr>
<th>Qtd</th>
<th>Item</th>
<th>Valor Unitário</th>
<th>Valor Total</th>
<th>Ação</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="qtd[]" id="qtd" placeholder="N°"></td>
<td><input type="text" name="item[]" id="item" placeholder="Descrição do Produto ou Serviço" /></td>
<td><input type="text" name="valoru[]" id="valoru" onblur="multiplica()" /></td>
<td><input type="text" name="valort[]" id="valort" /></td>
<td></td>
</tr>
</tbody>
</table>
I ended up riding based on tips from other users here. But I want to do the calculation of the input qtd
by multiplying the value of valoru
showing total in valort
But when I add another line, my code simply can not do the calculation (I guess it's because of the ID being repeated). What would be the best way to get around this?
Thanks to all who can help me. A warm hug and good coffee in this cold = P