2017-04-12 27 views
0

爲什麼我得到整行?如何獲得除最後一個表格以外的表格行數據

我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
</head> 
<body> 
<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Color</th> 
      <th>Size</th> 
      <th>Position</th> 
      <th>Print</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Dummy1.1</td> 
      <td>Dummy1.2</td> 
      <td>Dummy1.3</td> 
      <td>Dummy1.4</td> 
      <td><button>Print</button></td> 
     </tr> 
     <tr> 
      <td>Dummy2.1</td> 
      <td>Dummy2.2</td> 
      <td>Dummy2.3</td> 
      <td>Dummy2.4</td> 
      <td><button>Print</button></td> 
     </tr> 
    </tbody> 
</table> 
<script> 
    $(document).ready(function(){ 
     $('table tbody tr td:last-child').on('click', function(){ 
      var $row = $(this).parent('tr:not(:last-child)').text(); 
      console.log($row); 
     }); 
    }); 
</script> 
</body> 
</html> 

我想打印的行除了打印表格數據時,我克利克的按鈕。 我的代碼無法正常工作,因爲它顯示了我整行。

感謝您的幫助

+0

我發現解決方案var $ row = $(this).parent()。find('td:not(:last-child)')。text(); –

回答

0

你檢查是否tr不是最後一個孩子,並試圖找到與selector.It父母應該

$('table tbody tr td:last-child').on('click', function() { 
 
    var $row = $(this).parent().find('td:not(:last-child)').text(); 
 
    console.log($row); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table> 
 
    <thead> 
 
    <tr> 
 
     <th>Name</th> 
 
     <th>Color</th> 
 
     <th>Size</th> 
 
     <th>Position</th> 
 
     <th>Print</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>Dummy1.1</td> 
 
     <td>Dummy1.2</td> 
 
     <td>Dummy1.3</td> 
 
     <td>Dummy1.4</td> 
 
     <td> 
 
     <button>Print</button> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Dummy2.1</td> 
 
     <td>Dummy2.2</td> 
 
     <td>Dummy2.3</td> 
 
     <td>Dummy2.4</td> 
 
     <td> 
 
     <button>Print</button> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table>

相關問題