嗨,有一个表格可以在单击按钮时动态添加表格行,但现在我想在表格的列中执行计算,比如我有一个列名 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/