如何使用jQuery基于特定列或索引来计算总表?

时间:2015-02-24 07:11:38

标签: javascript jquery

我有一张像吹码一样的表:

<table class="somting">
  <tr>
     <th>Id</th>
     <th>Item</th>
     <th>Item Cost</th>
  </tr>
  <tr>
     <td>1</td>
     <td>Car</td>
     <td>200</td>
  </tr>
  <tr>
     <td>2</td>
     <td>Book</td>
     <td>500</td>
  </tr>
  <tr>
     <td>3</td>
     <td>Pen</td>
     <td>100</td>
  </tr>
 </table>

现在我的问题是如何获得项目成本列的子总计 用jquery

我的意思是如何添加200 + 500 + 100

6 个答案:

答案 0 :(得分:2)

您可以使用:nth-child()

sum=0;
$('.somting td:nth-child(3)').each(function(){
   sum += parseInt($(this).text());
})
//log sum

<强> Working Demo

答案 1 :(得分:1)

<table class="somting">
    <tr>
        <th>Id</th>
        <th>Item</th>
        <th>Item Cost</th>
    </tr>

    <tr>
        <td>1</td>
        <td>Car</td>
        <td>200</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Book</td>
        <td>500</td>
    </tr>
    <tr>
        <td>3</td>
        <td>Pen</td>
        <td>100</td>
    </tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
    $(document).ready(function() {
        var table = $("table.somting");
        var tds = table.find("tr td:nth-child(3)");

        var sum = 0;

        $.each(tds, function(i, me){
            var val = parseFloat($(me).text());
            sum += val;
        });

        var append = '<tr><td></td><td></td><td class="price"> '+sum+'</td></tr>';

        table.append(append);

    });
</script>

答案 2 :(得分:0)

您可以将项添加到项代码行,例如

  <tr>
     <td>1</td>
     <td>Car</td>
     <td class="price">200</td>
  </tr>

然后使用

var total = 0;
$(".price").each(function(){
    total += parseFloat( $(this).html());
});

请参阅http://jsfiddle.net/8wqppuo6

答案 3 :(得分:0)

试试这个

tot = 0;
$('table.somting tr').each(function(index, elem) {
    p = $(this).find(":nth-child(3)").html();
    p = parseInt(p);        
    if (!isNaN(p)) {          
      tot = tot + p;
    }               
});
console.log(tot);

答案 4 :(得分:0)

&#13;
&#13;
var sum =0;
$('table tr:not(:first)').each(function() {
  var lasttd=  $(this).find(':last-child')
   sum+=parseInt(lasttd.text());
});
alert(sum);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="somting">
  <tr>
     <th>Id</th>
     <th>Item</th>
     <th>Item Cost</th>
  </tr>
  <tr>
     <td>1</td>
     <td>Car</td>
     <td>200</td>
  </tr>
  <tr>
     <td>2</td>
     <td>Book</td>
     <td>500</td>
  </tr>
  <tr>
     <td>3</td>
     <td>Pen</td>
     <td>100</td>
  </tr>
 </table>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

使用此:

<script>
    $(document).ready(function() {
        var table = $("table.somting");
        var tds = table.find("tr td:nth-child(3)");
        var sum = 0;
            $.each(tds, function(i, me){
                var val = parseFloat($(me).text());
                sum += val;
            });
        var append = '<tr><td></td><td></td><td class="price"> '+sum+'</td></tr>';
        table.append(append);

    });

</script>