Changing a textbox within a textbox group / jQuery

Asked

Viewed 290 times

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);
        });


    });

2 answers

1


What you need is to iterate all fields by correctly matching their amount and value fields. Here’s a hint:

$(document).on('change', ".vlr_unit, .quantidade", function () {
    var total = 0;

    $(".vlr_unit").each(funtion() {
        var quantidade = $(this).siblings('.quantidade').val();
        total += calcula(this.value, quantidade);
    });

    $(".vlr_total").val(total);
});

What that code on top does is run through all the .vlr_unit and pair with the .quantidade respective. There each one increases the total with the return of the function calcula. At the end of all scrolling, send the result to .vlr_total

  • 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, 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.

  • @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

  • 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

0

$(".vlr_total").val(calcula($(".vlr_unit").val(), $(".quantidade").val())); tells jQuery to take the first element that contains the class .vlr_total and change its value. Already to change the value of all elements containing the class .vlr_total, you must use the function .each() jQuery, which will go through all found elements, not just the first. In its context it would translate to the following:

$(".vlr_total").each(function(){
    //this significa o elemento atual no loop
    $(this).val(calcula($(".vlr_unit").val(), $(".quantidade").val()));
});
  • in this case, the total vlr_total should be vlr_subtotal, at least this part I will put later and you already solved. What I would need for now is, when I click to add a field, comes out the product name, quantity, unit value and subtotal , I would like to know how to take the unit value and multiply by the quantity in the respective subtotal value field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.