1
I have a function that when clicking on a button, it would add fields, each time it adds, adds 3 fields, quantity, total value and subtotal value. My question is how would I change the total value when changing the quantity or value? These fields are n times. Only my question is in the matter of Javascript/jQuery, the database normally receives.
add_field() - Add fields as product name, unit value, quantity and total value
function add_field()
{
var f = $("#div_addfield");
f.append( '<br><hr><label for="nome_produto">Produto - ' + i + '</label> ' +
'<input id="nome_produto" type="text" name="data[OrdemCompra_itens][' + i + '][nome_produto]" class="form-control" /><br>' +
'<div class="form-inline">' +
'<label>Quant:</label>' +
' <input type="number" name="data[OrdemCompra_itens][' + i + '][quantidade]" min="1" value="1" ' +
' style="width:70px;" class="form-control quantidade" />' +
'<label>Vlr. Unit.:</label>' +
' <input type="text" name="data[OrdemCompra_itens][' + i + '][vlr_unit]" ' +
' style="width:100px;" class="form-control real vlr_unit" />' +
'<label>Vlr. Total.:</label>' +
' <input type="text" name="data[OrdemCompra_itens][' + i + '][vlr_total]" ' +
' style="width:120px;" class="form-control vlr_total" value="0,00" />' +
'</div><br>');
i++;
$(".real").maskMoney({showSymbol:true, symbol:"R$", decimal:",", thousands:"."});
}
calculates() function responsible for calculating, multiplying value by quantity
function calcula(vlr_unit, quant)
{
var res;
res = parseFloat(vlr_unit.replace(".", "").replace(",", ".")) * quant;
return parseFloat(res).toFixed(2).replace(".", ",");
}
Stretch that supposedly lies the error
$(document).on('change', ".vlr_unit, .quantidade", function () {
$(".vlr_total").val(calcula($(".vlr_unit").val(), $(".quantidade").val()));
});
In this last part, it only changes the first added field
Last correction, so far all right, just solving the decimal question
$(document).on('change', ".vlr_unit, .quantidade", function () {
var subtotal = 0, total = 0;
$(".vlr_unit").each(function() {
var quantidade = $(this).siblings('.quantidade').val();
subtotal = calcula(this.value, quantidade);
total += parseFloat(subtotal.replace(",", "."));
$("#vlr_total").val(total);
$(this).siblings(".vlr_subtotal").val(subtotal);
});
});
In this case, where the total value is the vlr_subtotal, the total I will do in an implementation later and this ends up helping me... every time changes the quantity or unit value, changes the value of the subtotal value that is a field next to each product
– braulio_holtz
@braulio_holtz, ok. Somehow I think my answer is the solution you need. If you put your HTML into jsFIddle, I can help better if it doesn’t work at first.
– Sergio
@braulio_holtz, I saw now that you never accepted an answer. Don’t forget to choose the best answer in each question and click on one to accept it. You can read here about how to do it: http://meta.pt.stackoverflow.com/q/1078/129
– Sergio
I got it here & #Xa;$(Document). on('change', ".vlr_unit, .quantity", Function() { var subtotal = 0, total = 0; $(".vlr_unit").each(Function() { var quantity = $(this).siblings('quantity').val(); subtotal = calculates(this.value, quantity); total += parseFloat(subtotal.replace(",", ".")); $("#vlr_total").val(total); $(this).siblings(".vlr_subtotal").val(subtotal); }); }); Thanks for the help, now I just have to see the part that makes the calculation does not set the standard x,xx
– braulio_holtz