如何在keyup事件中使用Jquery获取动态生成的表的单元格的行索引号?

时间:2018-09-27 09:11:50

标签: javascript jquery html-table

我有一个使用jquery自动生成的表。我需要在该表的单元格中的keyup事件上获取行的索引号。这就是我尝试过的。到目前为止没有运气。这是我的代码

jQuery

$(document).on('keyup change','.unit_price',function(){
        var tr = $(this).parents('tr');
        var price = $(this).val();          
        price = parseInt(price);
        var rownumber = $('#protable').index(tr);
        alert(rownumber);
}

HTML

<table id ="protable">
                <tr>
                    <th>Is Ordered</th> 
                    <th>Check List Item</th>
                    <th>Qty</th>
                    <th>Unit</th>
                    <th>Supplier</th>
                    <th>Unit Price</th> 
                    <th>Total Price</th>
                    <th>Remarks</th>        
                </tr>
 </table>

我的目标是通过乘以单价和数量来自动计算总价。请帮帮我

1 个答案:

答案 0 :(得分:0)

好吧,我终于明白了。实际上,这非常简单。

$(document).on('keyup change','.unit_price',function(){
    var tr = $(this).parents('tr');
    var price = $(this).val();          
    price = parseInt(price);
    var rownumber = tr.index();
}