最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sum TD in one TR - Stack Overflow

programmeradmin1浏览0评论

How can i sum values from td input in one TR?

<table>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="4"  /></td>
        <td><input type="text" class="three" value="5"  /></td>
        <td><input type="text" class="four" value="3"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="4"  /></td>
        <td><input type="text" class="three" value="5"  /></td>
        <td><input type="text" class="four" value="3"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="6"  /></td>
        <td><input type="text" class="three" value="4"  /></td>
        <td><input type="text" class="four" value="2"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="5" /></td>
        <td><input type="text" class="two" value="2"  /></td>
        <td><input type="text" class="three" value="3"  /></td>
        <td><input type="text" class="four" value="8"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="4"  /></td>
    </tr>    
</table>

LIVE DEMO

I can sum all values with function .each but how to make this for each tr, not td?

I would like sum values from input with class TWO and class THREE and class FOUR and add this to input with class SUM in same TR.

For my example should be:

2 4 5 3 **12** 2 // 4+5+3
2 4 5 3 **12** 2 // 4+5+3
2 6 4 2 **12** 2 // 6+4+2
5 2 3 8 **13** 4 // 2+3+8

How can i sum values from td input in one TR?

<table>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="4"  /></td>
        <td><input type="text" class="three" value="5"  /></td>
        <td><input type="text" class="four" value="3"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="4"  /></td>
        <td><input type="text" class="three" value="5"  /></td>
        <td><input type="text" class="four" value="3"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="2" /></td>
        <td><input type="text" class="two" value="6"  /></td>
        <td><input type="text" class="three" value="4"  /></td>
        <td><input type="text" class="four" value="2"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="2"  /></td>
    </tr>
    <tr class="here">
        <td><input type="text" class="one" value="5" /></td>
        <td><input type="text" class="two" value="2"  /></td>
        <td><input type="text" class="three" value="3"  /></td>
        <td><input type="text" class="four" value="8"  /></td>
        <td><input type="text" class="sum"  /></td>
        <td><input type="text" class="five" value="4"  /></td>
    </tr>    
</table>

LIVE DEMO

I can sum all values with function .each but how to make this for each tr, not td?

I would like sum values from input with class TWO and class THREE and class FOUR and add this to input with class SUM in same TR.

For my example should be:

2 4 5 3 **12** 2 // 4+5+3
2 4 5 3 **12** 2 // 4+5+3
2 6 4 2 **12** 2 // 6+4+2
5 2 3 8 **13** 4 // 2+3+8
Share Improve this question edited Jul 9, 2013 at 17:49 putvande 15.2k3 gold badges36 silver badges51 bronze badges asked Jul 9, 2013 at 17:42 user2565589user2565589 5831 gold badge4 silver badges6 bronze badges 2
  • 2 Please do not link to external resources that can vanish, thus making your question useless to others. Include a sample as part of your question. – Diodeus - James MacFarlane Commented Jul 9, 2013 at 17:44
  • 2 Have you made your own attempt? – George Commented Jul 9, 2013 at 17:45
Add a ment  | 

2 Answers 2

Reset to default 10

Your code should look like this.

You need to iterate over the td with the particular class in question and sum up the values. Then assign that value to the input that you were referring to.

$('tr.here').each(function(){
    var sum = 0;
    $('.two, .three, .four', this).each(function() {
        sum += parseFloat(this.value);
    });

    $('.sum', this).val(sum);
})

Check Fiddle

Try below code

$('tr.here').each(function () {
    var sum = parseFloat($(this).find('.two').val()) + parseFloat($(this).find('.three').val()) + parseFloat($(this).find('.four').val());

    $(this).find('.sum').val(sum);
})

Check this Fiddle

发布评论

评论列表(0)

  1. 暂无评论