Sum of values from different divs with the same class

后端 未结 6 1768
囚心锁ツ
囚心锁ツ 2020-12-13 18:15

I am loading dynamically divs that have a .totalprice class. At the end, I would like to sum of the values from all of the .totalprice.

6条回答
  •  执笔经年
    2020-12-13 19:03

    for non input elements:

    var sum = 0;
    $('.totprice').each(function(){
        sum = parseFloat(sum) + parseFloat($(this).text());
    });
    alert(sum);
    

    for input elements:

    var sum = 0;
    $('.totprice').each(function(){
        sum = parseFloat(sum) + parseFloat(this.value);
    });
    alert(sum);
    

提交回复
热议问题