【发布时间】:2011-10-10 00:45:54
【问题描述】:
考虑以下 HTML 表格:
<table id="myTable1">
<tr id="TR1">
<td><input type="text" id="quantity1" name="quantity1" /></td>
<td><input type="text" id="weight1" name="weight1" /></td>
<td><input type="text" id="sub_total1" name="sub_total1" /></td>
</tr>
</table>
我在这里想要完成的是,我需要根据在 中键入的值更新 每一行 上的 sub_total 字段的值每次触发 keyup() 时,数量 和 weight 字段在同一行。
现在,如果我正在处理的表只是静态的,我相信这将是一项非常易于管理的任务。但是包含动态添加表行给我带来了麻烦。
用于动态添加行的 JQuery:
$(document).ready(function() {
var counter = 2;
$("#addButton").click(function() {
$('#myTable1 tr:last').after(
'<tr id="TR"><td><input type="text" name="quantity' + counter +
'" id="quantity' + counter + '" value=""></input></td>' +
'<td><input type="text" name="weight' + counter +
'" id="weight' + counter + '" value=""></input></td>' +
'<td><input type="text" name="sub_total' + counter +
'" id="sub_total' + counter + '" value=""></input></td></tr>'
);
counter++;
});
});
这里有用于计算 sub_total 的公式:
var sub_total = ((170 * first 10 kilos) + (70 * the remaining weight)) * (quantity);
所以给定样本值: 数量 = 10 weight = 15,我们应该有
var sub_total = ((170 * 10) + (70 * 5)) * (10);
我有以下作为开始,但不太确定在这些函数中放置什么
$('#myTable1 input[id^=\'quantity\'], #myTable1 input[id^=\'weight\']').live('keyup',function() {
$('#myTable1 input[id^=\'quantity\'], #myTable1 input[id^=\'weight\']').each(function(index, value) {
});
});
【问题讨论】:
标签: javascript jquery jquery-plugins jquery-selectors