如何使用jquery在一个列中添加两行的值?

时间:2016-10-27 18:53:48

标签: javascript php jquery html html-table

code in ideone

我有一个名称roti。有12个不同的专栏。前7列采用不同的值并添加它们。然后名为TOTAL SALE(AMOUNT)的8列是前7列和两行的总和。我在下面给出了完整的代码。我实际上设法计算前6列。但是在第7栏我遇到了这个问题。它没有计算出我想要的实际结果。它增加了额外的0。

第二行值不与第8列相加

如何在总销售(金额)列中添加4p @ 14行值?

以下<script> </script>代码

中给出的实际计算结果
 <!DOCTYPE html>
<head>
    <title>Tortilla Daily Sell 1</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--for online also works on ofline-->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>
<body>
<table border="1">
    <tr>
        <th>Product Title</th>
        <th>PIECE and RATE</th>
        <td>DISTRIB. PACKET</td>
        <td>(BONUS) (PACKET)</td>
        <td>(REPLACE) (PACKET)</td>
        <td>(RETURN)(PACKET)</td>
        <td>TOTAL SALE (PACKET)</td>
        <td>SALES IN AMOUNT</td>
        <td>EXT. SALE (PACKET)</td>
        <td>TOTAL SALE (AMOUNT)</td>
        <td>DUE RECEIVED</td>
        <td>(DUE IN AMOUNT)</td>
        <td>(EXP. & COM.)</td>
        <td>TOTAL RECEIVED</td>
    </tr>
    <!--           ROTI  -->
    <tr class="product-row">
        <th rowspan="2">ROTI</th>
        <td>6p@20</td>
        <td>
            <input type="text"
                   class="form-control distrb-pak" id="distributedPacketForRoti6p20tk"
                   name='distributedPacketForRoti6p20tk'
                   value="0">

            <input type="hidden" class="price" name="price" value="20">
        </td>
        <td>
            <input type="text"
                   class="form-control bonus-pak" id="bonusForRoti6p20tk" name='bonusForRoti6p20tk'
                   value="0">
        </td>
        <td><input type="text"
                   class="form-control replace-pak" id="replaceForRoti6p20tk" name='replaceForRoti6p20tk'
                   value="0">
        </td>
        <td>
            <input type="text"
                   class="form-control return-pak" id="returnForRoti6p20tk" name='returnForRoti6p20tk'
                   value="0">
        </td>

        <td>
            <input type="text"
                   class="form-control total-sale-pak" id="totalSalePacketForRoti6p20tk"
                   name='totalSalePacketForRoti6p20tk'
                   value="0" readonly>

        </td>

        <td>
            <input type="text"
                   class="form-control sales-in-amount" id="salesInAmountForRoti6p20tk"
                   name='salesInAmountForRoti6p20tk'
                   value="0" readonly>
        </td>
        <td>
            <input type="text"
                   class="form-control extSale" id="extSaleForRoti6p20tk" name='extSaleForRoti6p20tk'
                   value="0">
        </td>
        <td rowspan="2">
            <input type="text"
                   class="form-control totalSale" id="totalSaleAmountForRoti"
                   name='totalSaleAmountForRoti'
                   value="0" readonly>
        </td>

        <td rowspan="2">

            <input type="text"
                   class="form-control dueReceived" id="dueReceivedForRoti" name='dueReceivedForRoti'
                   value="0">

        </td>

        <td rowspan="2">

            <input type="text"
                   class="form-control dueAmount" id="dueInAmountForRoti" name='dueInAmountForRoti'
                   value="0">

        </td>
        <td rowspan="2">

            <input type="text"
                   class="form-control expAndCom" id="expAndComForRoti" name='expAndComForRoti'
                   value="0">

        </td>
        <td rowspan="2">
            <input type="text"
                   class="form-control totalReceived" id="totalReceivedForRoti" name='totalReceivedForRoti'
                   value="0" readonly>

        </td>

    </tr>


    <!--           ROTI  -->
    <tr class="product-row">
        <td>4p@14</td>
        <td>
            <input type="text"
                   class="form-control distrb-pak" name='distributedPacketForRoti4p14tk'
                   id="distributedPacketForRoti4p14tk" value="0">
            <input type="hidden" class="price" name="price" value="14">
        </td>
        <td>
            <input type="text"
                   class="form-control bonus-pak" name='bonusForRoti4p14tk'
                   id="bonusForRoti4p14tk" value="0">
        </td>
        <td><input type="text"
                   class="form-control replace-pak" name='replaceForRoti4p14tk'
                   id="replaceForRoti4p14tk" value="0">
        </td>
        <td>
            <input type="text"
                   class="form-control return-pak" name='returnForRoti4p14tk'
                   id="returnForRoti4p14tk" value="0">
        </td>
        <td>
            <input type="text"
                   class="form-control total-sale-pak" name='totalSalePacketForRoti4p14tk'
                   id="totalSalePacketForRoti4p14tk" value="0" readonly>

        </td>

        <td>
            <input type="text"
                   class="form-control sales-in-amount" name='salesInAmountForRoti4p14tk'
                   id="salesInAmountForRoti4p14tk" value="0" readonly>

        </td>
        <td>
            <input type="text"
                   class="form-control extSale" id="extSalesForRoti4p14tk"
                   name='extSalesForRoti4p14tk'
                   value="0">
        </td>


    </tr>
    <script>

        $('.distrb-pak, .bonus-pak, .replace-pak, .return-pak, .dueReceived, .dueAmount, .extSale, .expAndCom').on('keydown keyup', function () {

            var distPakCount = $(this).parents('.product-row').find('.distrb-pak').val();
            var bonusPakCount = $(this).parents('.product-row').find('.bonus-pak').val();
            var replacePakCount = $(this).parents('.product-row').find('.replace-pak').val();
            var returnPakCount = $(this).parents('.product-row').find('.return-pak').val();
            var totalSalePak = $(this).parents('.product-row').find('.total-sale-pak');
            var salesInAmount = $(this).parents('.product-row').find('.sales-in-amount');
            var extSale = $(this).parents('.product-row').find('.extSale').val();
            var totalSale = $(this).parents('.product-row').find('.totalSale');
            var dueReceived = $(this).parents('.product-row').find('.dueReceived').val();
            var dueAmount = $(this).parents('.product-row').find('.dueAmount').val();
            var totalReceived = $(this).parents('.product-row').find('.totalReceived');
            var expCom = $(this).parents('.product-row').find('.expAndCom').val();
            var price = $(this).parents('.product-row').find('.price').val();


            var totalSalePakCount = parseInt(distPakCount) - parseInt(bonusPakCount) -
            parseInt(returnPakCount) - parseInt(replacePakCount);
            totalSalePak.val(totalSalePakCount);

            var salesInAmount_ = totalSalePakCount * parseInt(price);
            salesInAmount.val(salesInAmount_);

            var totalSaleInAmount = salesInAmount_ + parseInt(extSale);
            totalSale.val(totalSaleInAmount);

            var totalReceivedAmount = parseInt(totalSaleInAmount) + parseInt(dueReceived) - parseInt(dueAmount)
                - parseInt(expCom);

            totalReceived.val(totalReceivedAmount);
            //totalSale
        });


    </script>


</body>

这是输出。它在总销售列中增加了额外的0,并且当我在第2行中放置值时没有发生任何事情

enter image description here

1 个答案:

答案 0 :(得分:0)

循环遍历所有.totalReceived值并添加其值。然后将其放入.totalSale

var totalSale = 0;
$(".totalReceived").each(function() {
    totalSale += parseInt($(this).val());
}
$(".totalSale").val(totalSale);