开发者

Column total NAN error

开发者 https://www.devze.com 2022-12-14 20:34 出处:网络
I am using this bit of code to add the values in a table column which works pretty well until it encounters a null td cell with a  

I am using this bit of code to add the values in a table column which works pretty well until it encounters a null td cell with a   value. From that point on in the loop, I receive a NaN error in my alert. I'm wondering how to ignore these non-numeric values or replace them with zero for the calculation?

jQuery(function() {
        var MarketCapTotal = 0;
        // loop through the table
        jQuery('#grdWatchlistname tbody tr').each(function() {
        // replace the dollar signs and commas
        var MarketCap = (jQuery('td:nth-child(4)', jQuery(this)).html
().replace('$', '').replace(/[^a-zA-Z 0-9]+/g, ''));
            var td4th = jQuery('td:nth-child(4)', jQuery(this));
            MarketCapTotal += parseInt(MarketCap);
            alert(MarketCapTotal);
        });
    })开发者_开发技巧; 


Try this:

MarketCapTotal += isNaN(MarketCap) ? 0: parseInt(MarketCap, 10);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号