看看這是你腦子裏想的是什麼:http://jsfiddle.net/yaMRQ/23/
<table class="items">
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
<tr>
<td>Group:</td>
<td colspan="2" align="right" class="g_total"></td>
</tr>
</table>
<table class="items">
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
<tr class="targetfields">
<td><input type="text" name="quantity" class="quantity" /></td>
<td><input type="text" name="rate" class="rate" size="5" /></td>
<td><input type="text" name="sub_total" class="subtotal" /></td>
</tr>
<tr>
<td>Group:</td>
<td colspan="2" align="right" class="g_total"></td>
</tr>
</table>
<br/>---<br/>Total: <span id="total"></span>
$(function() {
$(".items").keyup(function(event) {
var total = 0;
$(".items").each(function(){
var gtotal = 0;
$(this).find(".targetfields").each(function() {
var qty = parseInt($(this).find(".quantity").val());
var rate = parseInt($(this).find(".rate").val());
var subtotal = qty * rate;
$(this).find(".subtotal").val(subtotal);
if(!isNaN(subtotal))
gtotal+=subtotal;
});
$(this).find(".g_total").html("EUR "+gtotal);
total+=gtotal
});
$("#total").html("EUR "+total);
});
})
所以有什麼問題?小提琴正在工作... –