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

查看:118
本文介绍了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天全站免登陆