Jquery 更新文本字段会更新表格单元格(每行)
我有一个表,用于向我的客户发送一个估计值,添加行,大致如下所示:Jquery 更新文本字段会更新表格单元格(每行),jquery,Jquery,我有一个表,用于向我的客户发送一个估计值,添加行,大致如下所示: <table> <thead> <tr> <th>Qty</th> <th>Kind</th> <th>Description</th> <th>Price</th>
<table>
<thead>
<tr>
<th>Qty</th>
<th>Kind</th>
<th>Description</th>
<th>Price</th>
<th>Discount</th>
<th>Tax</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="qty[]" class="qty" /></td>
<td><!-- select field with different kinds of services --></td>
<td><input type="text" name="desc[]" /></td>
<td><input type="text" name="price[]" class="price" /></td>
<td><input type="text" name="discount[]" class="discount" /></td>
<td><!-- select field with tax rates --></td>
<td class="total"><!-- here goes the total amount (qty * price - discount) --></td>
</tr>
</tbody>
</table>
<span class="subtotal"><!-- the total amount (all lines together) --></span>
数量
友善的
描述
价格
优惠
税
全部的
当input.qty、input.折扣和input.price上出现按键事件时,需要更新每行的td.total总金额。我有一个函数,可以在其中添加多个表行,从而得到输入数组
所以我需要的是,当输入字段被更新时,jQuery需要更新以下td.total。我尝试了两种方法,$('input.qty').keypress(function(){$(this).next('td.total').html()}),但都不起作用。选择器$(this)。next('td.total')
是错误的(因为td不是更改输入的同级)-尝试$('input.qty').keypress(function(){$(this.next('td.total').html())
[编辑]
实际上,脚本应该是
$('input.qty').live ('keypress', function(){
$(this).parent().siblings('.total').html()
});
[/edit]parent()不起作用,但它与parents()一起起作用,尽管它现在更新了所有td.total元素:(我已经用另一个建议更新了答案……应该更有效,添加了额外的rowsey!这很有魅力!在添加/删除元素时,我总是忘记live()函数!非常感谢:)