JS获取table中选中某几行其中某一列数值的总和

JS获取table中选中某几行其中某一列数值的总和
一、思路
1. 如何获取某几行,并且可以实时变化数值?实现如下:
$("input[type='checkbox']").click(function(){
    alert($(this).val());
})

2. 接下来就是实现当每次触发点击事件以后,然后,计算其中的值,实现如下:

复制代码
<script>
$(function(){
  $("input[type='checkbox']").click(function(){
    var total= getColumnTotal('productStoryForm',8);
    $('#total').html(total);
  })

  function getColumnTotal(tableId, columnIndex) {
    const table = document.getElementById(tableId);
    const rows = table.getElementsByTagName('tr');
    let total = 0;
    for (let i = 0; i < rows.length; i++) {
      const row = rows[i];
      const checkbox = row.getElementsByTagName('input')[0];
      if (checkbox && checkbox.type === 'checkbox' && checkbox.checked) {
        const cells = row.getElementsByTagName('td');
        const cellValue = cells[columnIndex].textContent;
        console.log(cellValue);
        total += parseFloat(cellValue);
      }
    }
    return total;
}
});
</script>
复制代码

以上就是实现动态计算每一列的值,并且展示在字段上,谢谢学习!!!

posted @   锦大大的博客呀!  阅读(728)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2021-04-28 Vue常见问题总结
2021-04-28 vue学习记录
2020-04-28 Autofac介绍和使用
点击右上角即可分享
微信分享提示