【问题标题】:calculate the percentage between two numbers inside HTML table TD计算 HTML 表格 TD 中两个数字之间的百分比
【发布时间】:2015-04-01 07:27:01
【问题描述】:

我的完整示例在JSFIDDLE

我有一个 HTML 表格,里面的值很少。我想根据其中的td 值找到每个tr 中的百分比,并且必须在每个tr 的末尾附加结果值。

我的 jQuery 代码

$('tr').each (function() {
    var frstCol = $(this).find('.combat').text();
    var seCol = $(this).find('.ac_yeild').text(); 
    var result = parseInt(frstCol) * 100 / parseInt(seCol);
    console.log(result);
    $('.res').text(result);
});

我如何计算第一列和第二列的百分比并将其放回我的result 列中每个tr

【问题讨论】:

    标签: javascript jquery html percentage


    【解决方案1】:

    使用这个:

    $(this).find('.res').text(result);//this will replace only specific result class 
    

    代替

    $('.res').text(result);//this is replacing all values of res class with new value
    

    DEMO

    注意:由于 td contains '--' Not a Number,您将获得 NAN 值。

    【讨论】:

      【解决方案2】:

      你需要为每个单独的列写下结果的值:

        $('tr').each (function() {
                  var frstCol = $(this).find('.combat').text();
                  var seCol = $(this).find('.ac_yeild').text(); 
                 alert(frstCol)
      
                     var result = (parseInt(frstCol) / parseInt(seCol))* 100 ;
                     console.log(result);
                     if(isNaN(result))
                       $(this).find('.res').text(0);
                     else
                       $(this).find('.res').text(result);
      
      
      
              });
      

      【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多