【问题标题】:How to get id of tr after clicking its td's element?点击 td 的元素后如何获取 tr 的 id?
【发布时间】:2015-02-24 09:34:52
【问题描述】:

我有一个 tr 列,就像这样。那么如何在单击更新后使用 jquery 获取 tr 的 id,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>

提前谢谢.. :)

【问题讨论】:

  • 您有点击处理程序吗...如果有,请查看.closest

标签: javascript php jquery html


【解决方案1】:

首先,如果此行重复,您应该在按钮上使用一个类,而不是id,因为重复的 id 属性无效。

<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>

在按钮上找到课程后,您可以使用closest() 找到tr

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

【讨论】:

  • 为什么使用 prop 而不是 attr?
  • @RoryMcCrossan 感谢它确实有效。也感谢你的建议:)
  • @AnilChaudhari 很高兴为您提供帮助
【解决方案2】:

你可以使用:

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

【讨论】:

  • how can I get the tr's id using jquery after clicking Update having id "quick-update-order-product"
【解决方案3】:

使用这个Closest()

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

【讨论】:

    猜你喜欢
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多