我有这个密码
我想在当前行的最后一列单元格中具有所有先前td单元格的总和。*在页面加载中,对于表中的所有行,如果可能的话,不使用类。
例如
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>3</td> <!-- this is empty cell which will on page load filled with SUM value !-->
</tr>
我怎样才能做到这一点 ?谢谢
您可以执行以下操作:
$('tr').find('td:last').text(function() {
return $(this).siblings().map(function() { return parseInt($(this).text(), 10); }).get().reduce(function(x, y) { return x + y });
});
找到td
每个数组的最后一个tr
,然后text
通过获取text
每个同级的来设置td
(使用map
),然后使用求和结果数组reduce()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句