2015-02-24 151 views
4

我有一個tr列,像這樣。然後,我怎麼能得到TR的id使用jQuery單擊更新ID爲「快速更新訂單產品」如何在點擊td的元素後獲得tr的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> 

在此先感謝後.. :)

+0

你有一個單擊處理.. 。如果是這樣結帳[.closest](http://api.jquery.com/closest) – 2015-02-24 09:35:30

回答

5

首先,如果重複此行,您應該使用按鈕上的類,而不是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'); 
}); 
+1

爲什麼使用道具而不是attr? – 2015-02-24 09:40:12

+0

@OliSoproniB。 http://stackoverflow.com/questions/5874652/prop-vs-attr – 2015-02-24 09:41:11

+0

@RoryMcCrossan謝謝他真的很有用。並感謝您的建議:) – 2015-02-24 09:48:52

2

您可以使用:

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

'我怎樣才能得到tr的id使用jquery點擊更新後有id「quick-update-order-product」' – 2015-02-24 09:37:23

1

使用此Closest()

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