首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在具有相似类名的表中减去值

在具有相似类名的表中减去值
EN

Stack Overflow用户
提问于 2014-01-28 12:38:38
回答 1查看 1.7K关注 0票数 0

我有一个表,要在其中减去列中的值。然而,它没有减去它不断加起来的值。

jsFiddle

HTML:

代码语言:javascript
复制
<table>
    <tr class="parent-ca">
        <td>
             <h4><em>Contract Amount</em></h4>

        </td>
        <td>
            <input type="text" class="rr" /> //all rr should be subtracted
        </td>
        <td>
            <input type="text" class="rr1" /> //all rr1 should be subtracted
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-abc">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-a">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-b">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-c">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="nettotal tt table-sub" data-minus="ca,contract,other2">
        <td>
             <h4>Net Cash</h4>

        </td>
        <td>
            <input type="text" id="rr" />
        </td>
        <td>
            <input type="text" id="rr1" />
        </td>
        <td>
            <input type="text" id="rr2" />
        </td>
        <td>
            <input type="text" id="rr3" />
        </td>
    </tr>
</table>

jQuery:

代码语言:javascript
复制
function calcSubMTotal(p) {
    var minus = 0;
    var minus_total = $('#' + p + '');


    $('.' + p + '').each(function (i) {
        if (!isNaN(this.value) && this.value.length !== 0) {
            minus -= this.value; //trying to subtract but this just adds up rather than subtract
        }
    });

    minus_total.val(minus);


}

$('input[type=text]').on('keyup', function () {
    calcSubMTotal("rr");
    calcSubMTotal("rr1");
    calcSubMTotal("rr2");
    calcSubMTotal("rr3");
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-28 13:13:59

更新的小提琴

Jquery

代码语言:javascript
复制
        function calcSubMTotal(p) {
    var minus = 0;
    var isFirst=true;
    var minus_total = $('#' + p + '');


    $('.' + p + '').each(function (i) {
        if (!isNaN(this.value) && this.value.length !== 0) {
            if(!isFirst)
            minus -= parseInt(this.value); 
            else
            {
                minus=parseInt(this.value);
                isFirst=false;
            }
        }
    });

    minus_total.val(minus);


}

$('input[type=text]').on('keyup', function () {
    calcSubMTotal("rr");
    calcSubMTotal("rr1");
    calcSubMTotal("rr2");
    calcSubMTotal("rr3");
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21405759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档