duan198299 2016-10-27 18:53
浏览 43
已采纳

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

code in ideone

I have a row name roti. there has 12 distinct column. First 7 column takes different values and add them. Then the 8 column named TOTAL SALE (AMOUNT) is the summation of first 7 column with two rows. I gave the full code below. I actually manage to calculate first 6 column. But in 7 column I have faced the problem. it didn't calculate the actual result that I want. It adds extra 0.

Second row value doesn't add up with the 8th column

How can I be able to add 4p@14 row values in total sale (amount) column?

Actual calculation given below in <script> </script> tag

 <!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>

This is the output. It adds extra 0 in total sale column and did not occur anything when I put value in row 2

enter image description here

  • 写回答

1条回答 默认 最新

  • duandang6111 2016-10-27 20:20
    关注

    Loop over all the .totalReceived values and add their values. Then put this into .totalSale:

    var totalSale = 0;
    $(".totalReceived").each(function() {
        totalSale += parseInt($(this).val());
    }
    $(".totalSale").val(totalSale);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站