问题
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.
Here is the function:
function sumOfColumns(tableID, columnIndex, hasHeader) {
var tot = 0;
$("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""))
.children("td:nth-child(" + columnIndex + ")")
.each(function() {
tot += parseInt($(this).html());
});
Do I need to stop the 'parseInt' piece?
回答1:
I'm assuming that you mean that the numbers now have commas as a thousands-separator, like this:
1234567 = "1,234,567"
You can remove all of those commas before you call parseInt, like this:
tot += parseInt($(this).html().replace(',',''));
来源:https://stackoverflow.com/questions/2456195/jquery-how-do-i-sum-a-column-of-numbers-with-commas