How to sum a single table column using Javascript?

后端 未结 2 1631
梦谈多话
梦谈多话 2021-01-02 16:08

I have a generated table on my JSP that contains data for transactions: each individual transaction is a row, and there\'s a column for category, amount, type, and descripti

2条回答
  •  温柔的废话
    2021-01-02 17:04

    Your variable tds should be named cls. Here is a working example.

    https://jsfiddle.net/34wf5gzs/

    for (var i = 0; i < cls.length; i++){
        if(cls[i].className == "countable"){
            sum += isNaN(cls[i].innerHTML) ? 0 : parseInt(cls[i].innerHTML);
        }
    }
    

    Roman C is right though. You're better off summing this up using a JS framework or using a counter in JSP. Custom scripts require some effort to maintain.

提交回复
热议问题