jquery - 从可见表列添加美元值

标签 jquery

我有一个包含大量信息的表,其中一个是充满货币值的列。它看起来像下面这样。

$23.21
$347.34
$3,200,884.00

我可以在服务器端添加值,但是使用过滤器,我不想再次访问服务器来获取信息。如何将这些值相加,以便在表格底部进行总计?

棘手的部分是,根据过滤器,我显示或隐藏某些值,因此我只想合计可见的货币值。

最佳答案

类似这样的东西(在不知道实际 html 的情况下无法精确)

var total = 0;

// the selector must be adjusted to the actual html
$('td.price:visible').each(function(){
   var value = $(this).text().slice(1).replace(/,/g,'');
   total += +value;
});

// use the total variable here to display it somewhere in your page

关于jquery - 从可见表列添加美元值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549099/

相关文章:

javascript - 使 Angular ng-if 保留空间以显示事件是否发生

javascript - jquery 随机选择后删除或设置对象中的 null 属性

javascript - 如何防止在图像损坏图标之前显示 ajax 成功?

javascript - 获取自定义事件的 "this"引用

javascript - jquery 类更改时更改 href 函数

jquery - 如何使用:eq selector in Nokogiri

jquery - jqgrid 获取所有网格列名称

javascript - 如何将多个 jQuery 函数嵌套到可访问的变量中?

javascript - 选择类时 jQuery 选择器速度较慢

jquery - 重力形式下拉动态人口问题