2015-04-03 87 views
0

我想,當用戶點擊一個排jQuery的如何獲得TD手機價值

下面任意位置即可發票號碼的價值是我的表

<div class="panel ui-widget-content" id="invoiceList"> 
<h2 class="ui-widget-header ui-corner-top" style="cursor: pointer; "><span>Invoices</span></h2> 
    <table cellspacing='0' id='header' class="ui-widget"> 
     <tr> 
      <th>Invoice Number</th> 
      <th>Invoice Total</th> 
     </tr>  
     <tr> 
      <td><a href="#" >INV-Error_Test1</a></td> 
      <td>22.000000 USD</td> 
     </tr> 
     <tr> 
      <td><a href="#" >INV-Error_Test2</a></td> 
      <td>22.000000 USD</td> 
     </tr> 
    </table> 
</div> 

下面是jQuery的我有這給只有當它的點擊Invoice Number領域的發票編號

$("#invoiceList td").click(function (e) { 
var result = $(this).text(); 
console.log('invoice --->'+result); 
}); 

http://jsfiddle.net/5n62md3m/

有人可以幫助我獲得發票號碼,當用戶點擊行中的任何地方時。

回答

1

另一種替代的解決方案可能是

$('#invoiceList tr:not(:first-child)').click(function(e){ 
    $tds = $(this).find("td"); 
    console.log($tds.eq(0).text()); 
}); 
1
$("#invoiceList tr:not(:first-child)").click(function (e) { 
    console.log('invoice ---> '+$(this).children('td:nth-child(1) a').text()); 
} 

一個小的獎金在這裏,如果你想獲得另一列,你只能改變數nth-child(和刪除後a,可能)。

2

檢查這一點,它可能是一個解決辦法:

$('tbody tr').on('click', function(e){ 
 
    var value = $(this).find('td:first-child a').text(); 
 
    alert(value); 
 
    e.stopPropagation(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class="panel ui-widget-content" id="invoiceList"> 
 
<h2 class="ui-widget-header ui-corner-top" style="cursor: pointer; "><span>Invoices</span></h2> 
 
    <table cellspacing='0' id='header' class="ui-widget"> 
 
     <thead> 
 
     <tr> 
 
      <th>Invoice Number</th> 
 
      <th>Invoice Total</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td><a href="#" >INV-Error_Test1</a></td> 
 
      <td>22.000000 USD</td> 
 
     </tr> 
 
     <tr> 
 
      <td><a href="#" >INV-Error_Test2</a></td> 
 
      <td>22.000000 USD</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div>

+0

的最後一個孩子是總。發票號碼是第一個孩子。 – Barmar 2015-04-03 22:29:56

+0

謝謝,我錯過了他想要發票號碼 – Charkan 2015-04-03 22:31:41