想必有一些元素(跨度?文本框?),您想要顯示小計。假設這是一個帶有「小計」id的跨度,然後將該項目文本設置爲小計只是多了一個行:基於OP小提琴
$rows.each(function(index) {
var $row = $(this);
$row.on('input', 'change', function(){
qty = $row.find("td:nth-child(2) input").val();
rate = $row.find("td:nth-child(3) input").val();
//convert the inputs to integers before multiplying,
//just to be sure
$('#sub-total').text(parseInt(qty) * parseInt(rate));
});
});
更新
var $rows = $("#tbl tbody tr");
$(".amount").attr('readonly');
$(".total").attr('readonly');
$rows.each(function(index) {
var $row = $(this);
$row.find('input').on('change', function(){
var qty = $row.find("td:nth-child(2) input").val();
var rate = $row.find("td:nth-child(3) input").val();
//convert the inputs to integers before multiplying,
var sub = parseInt(qty) * parseFloat(rate);
//only update the totals if the product is a number:
if(!isNaN(sub)){
$row.find('.total').val(sub);
var gTotal = 0;
$(".total").each(function(){
var t = parseFloat($(this).val());
gTotal += isNaN(t) ? 0 : t;
});
$('#grand_total input').val(gTotal)
}
});
});
小提琴here
我假設你的意思是綁定到鍵盤events..as你想要的總更新用戶的類型。你需要將你的更新函數綁定到'onkeyup'事件。這個.each函數只在加載時運行一次,但不會監聽任何用戶交互。查看jQuery的.on函數http://api.jquery.com/on/ –