簡體   English   中英

我需要查找表中一列的總值,不包括最后一行

[英]I need to find the total value of a column in a table, excluding the last row

我希望每當用戶對任何表單元格進行更改時,都能夠動態更新“新建”列下的總值。

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)

最終這對我有用:

函數getTotal(){

         var total = 0.0;

         $('table.ibody tbody tr').
         children('td.cl').
         children('input#totalable').each(function(){ 
             total += parseFloat(this.value);
         });

         $('table.ibody tbody tr').
         children('td.cl').
         children('div#totalable').html(total);

         alert(total);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM