jQuery does not add "+"

Asked

Viewed 275 times

1

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

When the user changes the quantity, for example 2 subtotal gets right, for example R$ 60,00, only the box has added up R$ 60,00 + Freight it 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));
                    }
                });
            })
        });
  • If you change the line var frete = $("#fretehidden").val(); for var frete = parseFloat($("#fretehidden").val());, what happens?

  • See if this helps you: http://jsfiddle.net/k5hdxs2c/

  • @Kaduamaral same thing.

  • (parseFloat('5')+parseFloat('7')). toFixed(2), and so on?

  • I took the . toFixed(2) and parseFloat() and just put them in some variables and it worked. Thank you!

  • Alisson, draft an answer showing what you did that worked. ;)

Show 1 more comment

1 answer

2


In doing:


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

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


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

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

Browser other questions tagged

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