jQuery does not add "+"

1

I'm doing a shopping cart with jQuery . The cart works like this: It has the products and all the values of each one. There is a field that changes the quantity. There is also another field of freight. After adding all the products it gives a subtotal of for example, $ 50.00 . When the person calculates the freight, for example R $ 20,00 . Then the subtotal is R $ 50.00 (products) and more R $ 20.00 of the freight. There is a box that is the sum of the two, in the case R $ 70.00 . When you change the quantity of products the value of box has to decrease or increase (it depends if the user is adding +1 or -1). The problem is:

When the user changes the quantity, for example 2 the subtotal is right, for example $ 60.00 , except that the box adds R $ 60,00 + Freight he only plays the Subtotal in the box and forgets to add the freight. My jQuery code is:

$(document).ready(function(){

            $("input#quantidade_carrinho").change(function(){

                var id_produto = $(this).attr('data-ref');
                var baseURL = $("#baseURL").val();
                var quantidade = $(this).val();

                var preco = $("#data-"+id_produto).val();
                var valor_total = parseFloat((preco * quantidade).toFixed(2));

                $("#total-"+id_produto).html('R$ '+parseFloat(valor_total).toFixed(2));

                $.ajax({

                    url: baseURL+'ajax/atualiza_carrinho',
                    type: 'POST',
                    data: {id:id_produto, quantidade:quantidade},

                    success:function(callbackAjax){

                        var callback = $.trim(callbackAjax);

                         var frete = $("#fretehidden").val(); //Pega o valor do Frete em um campo Hidden

                        var subtotal = parseFloat(callback).toFixed(2); //Pega o subtotal que está em um hidden (está funcionando)
                        var subtotalcomfrete = parseFloat(subtotal+frete).toFixed(2); //Aqui ele deveria somar o subtotal + frete só que não soma, só fica o subtotal

                        $(".subtotal-ajax").html('R$ '+ subtotal); //Funciona
                        $("#subtotal").val(subtotal); //Funciona

                        alert('Subtotal:' + subtotal+' - Subtotal com Frete '+subtotalcomfrete+' - Frete: '+frete); //Exibe por exemplo R$ 50,00 - R$ 50,00 - R$ 20,00... O que deveria aparecer é R$ 50,00 - R$ 70,00 - R$ 20,00

                        $('span.preco-total').html('R$ '+subtotalcomfrete);
                        $('span.valor_parcela').html(parseFloat(subtotalcomfrete/3).toFixed(2));
                    }
                });
            })
        });
    
asked by anonymous 18.09.2015 / 21:06

1 answer

2

When doing:


var subtotalcomfrete = parseFloat(subtotal+frete).toFixed(2);

You are concatenating a number with a string. The correct would be:


var subtotalcomfrete = subtotal + parseFloat(frete).toFixed(2);

Also be careful with "." and "," in the input fields. You should always use "." to separate the decimals

    
18.09.2015 / 21:33