根据类对表列的所有值求和

时间:2021-02-16 07:56:52

I'm trying to retrieve the sum of all values in a td based on a specific class. The code does not throw up any errors but my sum keeps resulting in "0".

我正在尝试根据特定的类检索td中所有值的总和。代码不会抛出任何错误,但我的总和会导致“0”。

Do the numerical values have to be specified in a particular way? I saw some other answers here on SO from where have imitated the code, and i dont see any real difference between mine and theirs so im confused as to why mine isnt working.

是否必须以特定方式指定数值?我在这里看到了一些其他的答案,从那里模仿代码,我没有看到我和他们之间有任何真正的区别,所以我很困惑为什么我的工作不起作用。

Here is a tutorial i followed for reference: http://viralpatel.net/blogs/2009/07/sum-html-textbox-values-using-jquery-javascript.html

以下是我参考的教程:http://viralpatel.net/blogs/2009/07/sum-html-textbox-values-using-jquery-javascript.html

Here is my javascript

这是我的javascript

$(document).ready(function(){
$('.price').each(function() {
    calculateSum();
});
});

 function calculateSum() {

var sum = 0;
//iterate through each td based on class and add the values
    $(".price").each(function() {

        //add only if the value is number
        if(!isNaN(this.value) && this.value.length!=0) {
            sum += parseFloat(this.value);
        }

    });
$('#result').text(sum);    
};

Here is my html

这是我的HTML

<table border="1">
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Banana</td>
<td class ="price">50</td>
</tr>
<tr>
 <td>Apple</td>
 <td class ="price">100</td>
</tr>
</table>

<div id="result"></div>

2 个解决方案

#1


37  

You want to use text() instead of this.value (since <td>s don't have a "value"):

你想使用text()而不是this.value(因为 s没有“value”):

var sum = 0;
// iterate through each td based on class and add the values
$(".price").each(function() {

    var value = $(this).text();
    // add only if the value is number
    if(!isNaN(value) && value.length != 0) {
        sum += parseFloat(value);
    }
});

Also, you're looping over your .price elements (calling calculateSum) multiple times. You can replace

此外,您循环遍历.price元素(调用calculateSum)多次。你可以替换

$(document).ready(function(){
    $('.price').each(function() {
        calculateSum();
    });
});

with

$(calculateSum);

#2


3  

I have a JQuery plugin, Sumtr, that handles this pretty well if you want a summary row.

我有一个JQuery插件,Sumtr,如果你想要一个摘要行,它可以很好地处理这个问题。

Here is your example with the plugin.

以下是插件的示例。

#1


37  

You want to use text() instead of this.value (since <td>s don't have a "value"):

你想使用text()而不是this.value(因为 s没有“value”):

var sum = 0;
// iterate through each td based on class and add the values
$(".price").each(function() {

    var value = $(this).text();
    // add only if the value is number
    if(!isNaN(value) && value.length != 0) {
        sum += parseFloat(value);
    }
});

Also, you're looping over your .price elements (calling calculateSum) multiple times. You can replace

此外,您循环遍历.price元素(调用calculateSum)多次。你可以替换

$(document).ready(function(){
    $('.price').each(function() {
        calculateSum();
    });
});

with

$(calculateSum);

#2


3  

I have a JQuery plugin, Sumtr, that handles this pretty well if you want a summary row.

我有一个JQuery插件,Sumtr,如果你想要一个摘要行,它可以很好地处理这个问题。

Here is your example with the plugin.

以下是插件的示例。