javascript - 我需要找到表中一列的总值,不包括最后一行

标签 javascript jquery html jsp struts2

每当用户对任何表格单元格进行更改时,我希望能够动态更新"new"列下的总值。

DOM:

<div id='lc_adjustments'>
  <table id='adjustments' cellspacing='0' width='550px' class='lc_nf' border='0'
  cellpadding='0'>
    <tbody>
      <tr>
        <td>
          <table cellspacing='0' width='100%' class='lcb' border='0' cellpadding='0'>
            <tr>
              <td>
                <table cellspacing='1' width='100%' class='ibody' border='0' cellpadding='0'>
                  <colgroup>
                    <col width='250px'></col>
                    <col width='150px'></col>
                    <col width='150px'></col>
                  </colgroup>
                  <tr class='header'>
                    <td>Item Name</td>
                    <td>Current Value</td>
                    <td>New Value</td>
                  </tr>
                  <tr onmouseover='high(this);' class='even' onmouseout='low(this);'>
                    <td class='cl'>Item number 1</td>
                    <td class='cl'>89.50</td>
                    <td class='cl'>
                      <input class='clsMandatoryReal' onchange='getTotal();' id='totalable'
                      value='89.50' size='25' type='text'>
                    </td>
                  </tr>
                  <tr onmouseover='high(this);' class='odd' onmouseout='low(this);'>
                    <td class='cl'>Item number 2</td>
                    <td class='cl'>69.70</td>
                    <td class='cl'>
                      <input class='clsMandatoryReal' onchange='getTotal();' id='totalable'
                      value='69.70' size='25' type='text'>
                    </td>
                  </tr>
                  <tr onmouseover='high(this);' style='font-weight: bold;' onmouseout='low(this);'
                  class='even'>
                    <td class='cl'>TOTAL STOCK MASS</td>
                    <td class='cl'>?</td>
                    <td class='cl'>?</td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </tbody>
  </table>

我尝试在 jquery 中执行此操作,但我的函数似乎不起作用:

function getTotal() {

  var total = 0.0;

  //iterate over each of the cells
  $('table.ibody tbody tr').
  children('td.cl').
  children('input#totalable:not(:last)').each(function () {

    total += $(this).text();
  });

  $('table.ibody tbody tr').
  children('td.cl').
  children('input#totalable:last)').value = total;

  alert(total);

}

我愿意接受 jquery 或 javascript 中的解决方案。 html 部分由 Struts 生成,这意味着在大多数情况下我对 html class/id 属性无能为力。

最佳答案

这是一个 jQuery 替代方案:

var total = 0;

//Iterate all td's in second column
$('#adjustments>tbody>tr>td:nth-child(2)').each( function(){
   total += parseFloat($(this).text()) || 0;       
});

alert(total)

关于javascript - 我需要找到表中一列的总值,不包括最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321469/

相关文章:

javascript - 如何使用 JavaScript 或 HTML 将文件输入设置为空?

javascript - 设置复选框事件定期检查

javascript - 如何设置 Daterangepicker 选择的日期选择器 1 之后最多 9 天

javascript - Amcharts - 当图表滚动条滚动时可排序的 div 移动

javascript - 在 Firefox 插入符号中,始终出现在可编辑的 div 中

javascript - 查找当前类是否包含单词,然后将新类附加到这些 li 中 - jquery

javascript - Bootstrap 折叠菜单

javascript - 在二维数组中查找比较

javascript - 获取 HTML 或 JavaScript 中的服务器计算机名称

javascript - 多次更改元素的 ID