来自相同类别的不同div的值的总和 [英] Sum of values from different divs with the same class

查看:43
本文介绍了来自相同类别的不同div的值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态加载具有.totalprice类的div.最后,我想对所有.totalprice的值求和.

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.

推荐答案

对于<div>元素:

For <div> Elements:

var sum = 0;
$('.totalprice').each(function(){
    sum += parseFloat($(this).text());  // Or this.innerHTML, this.innerText
});

您可以在此处看到工作示例

对于<input>元素(输入,复选框等):

For <input> Elements (inputs, checkboxes, etc.):

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

或者,如果要查找整数,则可以使用 parseInt() 函数.

Alternatively, if you are looking for an integer, you can use the parseInt() function.

您可以在此处查看此示例的工作示例.

这篇关于来自相同类别的不同div的值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆