jQuery:如何用逗号对一列数字求和? [英] jQuery: How do I sum a column of numbers with commas?

查看:20
本文介绍了jQuery:如何用逗号对一列数字求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了我在网上找到的以下功能,它运行良好.但是,当我的用户后来要求在数字中包含逗号时,它就坏了.它只添加逗号前面的数字.

I used the following function I found online and it works perfectly. However, when my user later asked for commas to be included in the numbers, it broke. It only adds the numbers preceding the comma.

函数如下:

function sumOfColumns(tableID, columnIndex, hasHeader) {
            var tot = 0;
            $("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""))
              .children("td:nth-child(" + columnIndex + ")")
              .each(function() {
                  tot += parseInt($(this).html());
              });

我需要停止 'parseInt' 片段吗?

Do I need to stop the 'parseInt' piece?

推荐答案

我假设你的意思是数字现在有逗号作为千位分隔符,像这样:

I'm assuming that you mean that the numbers now have commas as a thousands-separator, like this:

1234567 = "1,234,567"

您可以在调用 parseInt 之前删除所有这些逗号,如下所示:

You can remove all of those commas before you call parseInt, like this:

tot += parseInt($(this).html().replace(',',''));

这篇关于jQuery:如何用逗号对一列数字求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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