0

嗨,有一个表格可以在单击按钮时动态添加表格行,但现在我想在表格的列中执行计算,比如我有一个列名 prevqty、thisqty、qty(这是总数量)、费率和金额,我有一个存储总金额的总字段我已经尝试过计算,但它似乎不起作用

这是我的脚本

   $(document).ready(function() {
var currentItem = $('#items').val();

$('#data').on('keyup', '.prevqty, .thisqty, .qty, .rate, .cal', calculateRow);

$('#addnew').click(function() {
    currentItem++;
    $('#items').val(currentItem);
    $('#data').append('<tr>\n\
        <td align="center"><input type="text" size="6" maxlength="6" maxlength="6" name="ord_' + currentItem + '" class="form-input-oth" onkeyup="return copy(this.val());"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_' + currentItem + '" class="form-input-oth"/></td>\n\
        <td align="center"><textarea name="descrip_' + currentItem + '" cols="70" class="form-input-textareasm"></textarea></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="unit_' + currentItem + '" class="form-input-rate"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="prevqty_' + currentItem + '" class="prevqty form-input-rate" onkeyup="calculateRow();"//></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="thisqty_' + currentItem + '" class="thisqty form-input-rate" onkeyup="calculateRow();"//></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="qty_' + currentItem + '" class="qty form-input-rate" onkeyup="calculateRow();"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="rate_' + currentItem + '" class="rate form-input-rate" onkeyup="calculateRow();"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="amt_' + currentItem + '" class="cal  form-input-amt" onkeyup="calculateRow();"/></td>\n\
        </tr>'
                     );
});

function calculateSum() {
    var sum = 0;
    $(".cal").each(function () {
        if (!isNaN(this.value) && this.value.length != 0) {
            sum += parseFloat(this.value);
        }
    });
    $("#total").val(sum.toFixed(2));
}
function calculateRow() {
    var cost = 0;
    var $row = $(this).closest("tr");
    var prevqty = parseFloat($row.find('.prevqty').val());
    var thisqty = parseFloat($row.find('.thisqty').val());
    var qty = parseFloat($row.find('.qty').val(prevqty + thisqty));
        //parseFloat($row.find('.qty').val());
    alert(qty);
    if (isNaN(qty)) {
        $row.find('.qty').val("0");
    } else {
        $row.find('.qty').val(qty);
    }
    // changed the following line to only look within the current row
    var rate = parseFloat($row.find('.rate').val());

    cost = qty * rate;

    if (isNaN(cost)) {
        $row.find('.cal').val("0");
    } else {
        $row.find('.cal').val(cost);
    }
    calculateSum();
}});

我在这里有一个工作小提琴,prevqty 和 thisqty 工作正常,但费率和金额计算不起作用,请参见此处http://jsfiddle.net/rey_kahn/YS4N2/

4

2 回答 2

2

每次您的$("#addnew").click事件触发时,您都需要重新绑定该keyup/calculateRow方法。就像是:

$('#addnew').click(function() {
    currentItem++;
    $('#items').val(currentItem);
    $('#data').append('<tr>\n\
        <td align="center"><input type="text" size="6" maxlength="6" maxlength="6" name="ord_' + currentItem + '" class="form-input-oth" onkeyup="return copy(this.val());"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_' + currentItem + '" class="form-input-oth"/></td>\n\
        <td align="center"><textarea name="descrip_' + currentItem + '" cols="70" class="form-input-textareasm"></textarea></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="unit_' + currentItem + '" class="form-input-rate"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="prevqty_' + currentItem + '" class="prevqty form-input-rate" onkeyup="calculateRow();"//></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="thisqty_' + currentItem + '" class="thisqty form-input-rate" onkeyup="calculateRow();"//></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="qty_' + currentItem + '" class="qty form-input-rate" onkeyup="calculateRow();"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="rate_' + currentItem + '" class="rate form-input-rate" onkeyup="calculateRow();"/></td>\n\
            <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="amt_' + currentItem + '" class="cal  form-input-amt" onkeyup="calculateRow();"/></td>\n\
        </tr>'
                     );
    $('#data').off('keyup').on('keyup', '.prevqty, .thisqty, .qty, .rate, .cal', calculateRow);
});

$("#data").off('keyup').on(...在活动结束时注意到了吗?那应该使您的新行正确计算。

更新

查看您在 jsfiddle 评论中发布的代码后,我发现了一些 HTML 错误,但我认为真正的问题是您设置/计算的方式qty。看看这个更新小提琴:http: //jsfiddle.net/YS4N2/1/

在那里你会看到我将 qty 输入与它用于计算的方式分开设置。这工作正常。

于 2013-04-12T12:25:45.187 回答
0

在开头添加了 Default Row 和 Grand Total 。

以及一些细微的变化。

<table border="1px" width="90%" id="data">                  
              <tr>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Order</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Sr No.</font><span></span></label></td>
                 <td width="580px" align="center"><label for=""><font color="#0099FF" size="3px">Description</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Unit</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Previous Qty.</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">This Bill Qty</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Total Quantity</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Rate</font><span></span></label></td>
                 <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Amount</font><span></span></label></td>
              </tr>
               <tr>
                <td><input type="text" size="6" maxlength="6" maxlength="6" name="ord_' + currentItem + '" class="form-input-oth" onkeyup="return copy(this.val());"/></td>
                <td><input type="text" size="6" maxlength="6" maxlength="6" name="srno_' + currentItem + '" class="form-input-oth"/></td>     
                <td><textarea name="descrip_' + currentItem + '" cols="70" class="form-input-textareasm"></textarea></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="unit_' + currentItem + '" class="form-input-rate"/></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="prevqty_' + currentItem + '" class="prevqty "/></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="thisqty_' + currentItem + '" class="thisqty "/></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="qty_' + currentItem + '" class="qty "/></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="rate_' + currentItem + '" class="rate "/></td>     
                <td><input type="text" size="6" maxlength="9" maxlength="6" name="amt_' + currentItem + '" class="cal"/></td>     
              </tr>
              <tr>
                <td colspan="8"></td>
                <td><input type="text" id="total"/></td>
              </tr>


检查更新链接:http: //jsfiddle.net/YS4N2/9/

于 2016-04-27T09:26:34.947 回答