最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get id of tr after clicking its td's element? - Stack Overflow

programmeradmin1浏览0评论

I have a tr column, like this. Then how can I get the tr's id using jquery after clicking Update having id "quick-update-order-product"

<tr id="product-id-<?=$product->product->id;?>">            
        <td><span class="product_price_show"></span></td>
        <td><span><?=  $product->product_qty; ?></span></td>
        <td><span><?= $product->product->stock->qty; ?></span></td>
        <td><span><?=  $product->total_price_tax_incl; ?></span></td>
        <td>
            <a class="quick-update-order-product">Update</a>                
        </td>            
</tr>

Thanks in advance.. :)

I have a tr column, like this. Then how can I get the tr's id using jquery after clicking Update having id "quick-update-order-product"

<tr id="product-id-<?=$product->product->id;?>">            
        <td><span class="product_price_show"></span></td>
        <td><span><?=  $product->product_qty; ?></span></td>
        <td><span><?= $product->product->stock->qty; ?></span></td>
        <td><span><?=  $product->total_price_tax_incl; ?></span></td>
        <td>
            <a class="quick-update-order-product">Update</a>                
        </td>            
</tr>

Thanks in advance.. :)

Share Improve this question edited Feb 24, 2015 at 9:49 Anil Chaudhari asked Feb 24, 2015 at 9:34 Anil ChaudhariAnil Chaudhari 84114 silver badges31 bronze badges 1
  • do you have a click handler... if so checkout .closest – Arun P Johny Commented Feb 24, 2015 at 9:35
Add a ment  | 

3 Answers 3

Reset to default 8

Firstly, if this row is repeated you should use a class on the button, not an id, as duplicate id properties are invalid.

<tr id="product-id-<?=$product->product->id;?>">            
    <td><span class="product_price_show"></span></td>
    <td><span><?=  $product->product_qty; ?></span></td>
    <td><span><?= $product->product->stock->qty; ?></span></td>
    <td><span><?=  $product->total_price_tax_incl; ?></span></td>
    <td>
        <a class="quick-update-order-product">Update</a>                
    </td>            
</tr>

Once you've got the class on the button you can use closest() to find the tr:

$('.quick-update-order-product').click(function() {
    var trId = $(this).closest('tr').prop('id');
});

You can use:

$('td a').click(function(){
  alert($(this).closest('tr').attr('id'));
});

Use this Closest()

$('.quick-update-order-product').click(function() {
    var trId = $(this).closest('tr').attr('id');
});
发布评论

评论列表(0)

  1. 暂无评论