I can't get the sum of grandtotal in jquery

时间:2015-12-10 01:37:10

标签: javascript jquery arrays

How to get the grand total. Just like the link of the picture below, it only display the amount input and display it beside the other amount. How can I add the grand total.

this is the link of the picture. please click. enter image description here

grandtotal = 0;
$('input[name^=amount]').each(function() {
    //alert($(this).val());
    grandtotal += $(this).val();
});
$("#grandtotal").html(grandtotal);

1 个答案:

答案 0 :(得分:4)

you're adding strings together, rather than numbers. If you want a number to display, you'll need to use:

//javascript
grandtotal += parseFloat($(this).val());

or, since you added [php] tag:

//php
grandtotal += floatval( $your_variable );
相关问题