Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery-计算列“的平均值”;价值观;然后把它放在另一列的每一个td中_Jquery_Jquery Ui_Jquery Plugins - Fatal编程技术网

Jquery-计算列“的平均值”;价值观;然后把它放在另一列的每一个td中

Jquery-计算列“的平均值”;价值观;然后把它放在另一列的每一个td中,jquery,jquery-ui,jquery-plugins,Jquery,Jquery Ui,Jquery Plugins,假设我有一张这样的桌子: <table cellpadding="0" cellspacing="0" border="0" class="display" id="selection"> <th>Values</th> <th>Average</th> <tbody> <tr> <td>5</td>

假设我有一张这样的桌子:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="selection">
    <th>Values</th>
    <th>Average</th>
    <tbody>
        <tr>
            <td>5</td>
            <td></td>       
        </tr>   
        <tr>
            <td>0.9</td>
            <td></td>       
        </tr>  
        <tr>
            <td>2</td>
            <td></td>       
        </tr>  
        <tr>
            <td>6</td>
            <td></td>       
        </tr>  
    </tbody>
</table>

价值观
平均值
5.
0.9
2.
6.
我想计算列“values”的平均值,并使用Jquery将其放入平均列的每个td中。结果应该如下图所示:

请注意,我使用搜索框筛选表中的结果,每次用户搜索时,行数都会更改

var sum = 0,
    count = -1,
    all = $('#selection > tbody > tr');
all.each(function() {
    sum += +$('td:eq(0)', this).text();
    count++;
});
all.find('td:eq(1)').text((sum / count).toFixed(3));

+$('td:eq(0)”,this).text()
,这里是开头的
+
将文本转换为数字

var sum = 0, rows = $('#selection tbody tr');

rows.each(function() {
  sum += parseFloat($(this).children('td').eq(0).text());
});

rows.each(function() {
  var tds = $(this).children('td'), value = parseFloat(tds.eq(0).text());
  $(this).children('td').eq(1).text((value/sum).toFixed(3));
});

+$('td:eq(0)”,this).text()
,这里是开头的
+
将文本转换为数字

var sum = 0, rows = $('#selection tbody tr');

rows.each(function() {
  sum += parseFloat($(this).children('td').eq(0).text());
});

rows.each(function() {
  var tds = $(this).children('td'), value = parseFloat(tds.eq(0).text());
  $(this).children('td').eq(1).text((value/sum).toFixed(3));
});


xmm请向我们展示您的一些代码。xmm请向我们展示您的一些代码。我在rowcount方面遇到了问题,现在我有了解决方案。非常感谢。为了了解更多,有没有控制小数的方法?。例如,显示带2位小数的平均列?@DavidPeterson-yup。。只需将
更改为fixed(2)
,这将显示2个十进制值我的行计数有问题,现在我有了解决方案。非常感谢。为了了解更多,有没有控制小数的方法?。例如,显示带2位小数的平均列?@DavidPeterson-yup。。只需将
更改为fixed(2)
,这将显示2个十进制值