使用jQuery对表列求和

时间:2016-12-15 14:47:41

标签: javascript jquery html datatable

我动态创建了table。页面加载时有一列为空,但是当我选择下拉菜单(其他列)的数量和默认价格值(其他列)时,它会填充值。我想使用jQuery添加此过程创建的总值,并显示总值。

有没有人有任何想法我怎么能这样做?我已经尝试了很多,但没有结果出现。

我试试这个:

$(document).each("[id^=total]", function(event){
  var num = this.id.slice(5);
  var sum = 0;

     var value = $('#total'+num).text();
     sum += parseFloat(this.value);

     $('#sum').text(sum);
 });

HTML(将显示结果):

<tr>
   <th colspan="6" style="text-align:right">Total:</th>
   <th colspan="2" style="text-align:center"><span id="sum"></span></th>
</tr>

HTML(id =&#39; qua2&#39;是1到20的下拉菜单):

<tr>
    <td><center><selectclass='choose'id='qua2'></select></center></td>
    <td><center><spanid='yprice2'>101.10</span></center></td>
    <td><center><spanid='total2'></span></center></td>
</tr>

注意:

在创建表后创建列total。我选择一个下拉值并进行此过程(下拉值*价格).Result显示在列总数中。

更新(我使用此脚本,但id='sum'中的结果为NaN):

$(document).on('change', "[id^=qua]", function(event){
var num = this.id.slice(3);
var sum = 0;
var value = $("#qua"+num).val();
var yprc = $("#yprice"+num).text();
var amount = value * yprc;
amount = amount.toFixed(2);

var $sum = $("#sum");
var $total = $("#total"+num);

    $total.html(amount);

 $("[id^=total]").each(function(){
 sum += parseFloat($(this).text());
 });
 $sum.text(sum);
});

2 个答案:

答案 0 :(得分:3)

<强> Working fiddle

我建议避免使用id并改为使用类:

calc_total();

$(".choose").on('change', function(){
  var parent = $(this).closest('tr');
  var price  = parseFloat($('.price',parent).text());
  var choose = parseFloat($('.choose',parent).val());

  $('.total',parent).text(choose*price);

  calc_total();
});

function calc_total(){
  var sum = 0;
  $(".total").each(function(){
    sum += parseFloat($(this).text());
  });
  $('#sum').text(sum);
}

希望这有帮助。

&#13;
&#13;
calc_total();

$(".choose").on('change', function(){
  var parent = $(this).closest('tr');
  var price  = parseFloat($('.price',parent).text());
  var choose = parseFloat($('.choose',parent).val());

  $('.total',parent).text(choose*price);

  calc_total();
});

function calc_total(){
  var sum = 0;
  $(".total").each(function(){
    sum += parseFloat($(this).text());
  });
  $('#sum').text(sum);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border=1>
  <tr>
    <td>
      <center>
        <select class='choose'>
          <option value='1'>1</option>
          <option value='2'>2</option>
          <option value='3'>3</option>
        </select>
      </center>
    </td>
    <td>
      <center>
        <span class='price'>100</span>
      </center>
    </td>
    <td>
      <center>
        <span class='total'>100</span>
      </center>
    </td>
  </tr>
  <tr>
    <td>
      <center>
        <select class='choose'>
          <option value='1'>1</option>
          <option value='2'>2</option>
          <option value='3'>3</option>
        </select>
      </center>
    </td>
    <td>
      <center>
        <span class='price'>100</span>
      </center>
    </td>
    <td>
      <center>
        <span class='total'>100</span>
      </center>
    </td>
  </tr>
  <tr>
    <td>
      <center>
        <select class='choose'>
          <option value='1'>1</option>
          <option value='2'>2</option>
          <option value='3'>3</option>
        </select>
      </center>
    </td>
    <td>
      <center>
        <span class='price'>100</span>
      </center>  
    </td>
    <td>
      <center>
        <span class='total'>100</span>
      </center>
    </td>
  </tr>
  <tr>
    <th colspan="2" style="text-align:right">Total:</th>
    <th colspan="2" style="text-align:center"><span id="sum"></span></th>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试这种方式:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Sum Total for Column in jQuery </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $('table thead th').each(function (i) {
                calculateColumn(i);
            });
        });
        function calculateColumn(index) {
            var total = 0;
            $('table tr').each(function () {
                var value = parseInt($('td', this).eq(index).text());
                if (!isNaN(value)) {
                    total += value;
                }
            });
            $('table tfoot td').eq(index).text('Total: ' + total);
        }
    </script>
</head>

<body>
    <table id="sum_table" width="300" border="1">
        <thead>
            <tr>
                <th>ITEM 1</th>
                <th>ITEM 2</th>
                <th>ITEM 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td>Total:</td>
                <td>Total:</td>
                <td>Total:</td>
            </tr>
        </tfoot>
    </table>
</body>

</html>

希望有帮助。