更新文本字段会更新表格单元格(每行)

时间:2009-09-16 14:48:02

标签: jquery

我有一个表格,用于在估算中添加行以发送给我的客户,大致看起来像这样:

<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.discount和input.price上有按键事件时,需要更新td.total以获取每行的总和。我有一个函数,我可以添加几个表行,因此输入数组。

所以我需要的是当输入字段更新时,jQuery需要更新以下td.total。我试过两个,$('input.qty')。keypress(function(){$(this).next('td.total')。html()})但是没有用。

1 个答案:

答案 0 :(得分:2)

选择器$(this).next('td.total')是错误的(因为td不是更改输入的兄弟) - 尝试使用$('input.qty').keypress(function(){ $(this).next('td.total').html() })

[编辑]

实际上脚本应该是

$('input.qty').live ('keypress', function(){
    $(this).parent().siblings('.total').html()
});

[/编辑]