2013-08-16 42 views
-2

我有一張桌子和裏面的表我有10行我想找到一個基於id的特定行,我也想找到旁邊的其他行。查找控制旁邊的特定tr

例如

<table> 
     <tr>1</tr> 
     <tr>2</tr> 
     <tr>3</tr> 
     <tr id="CurrentId">4</tr> 
     <tr> 
      <td>5</td> 
     </tr> 
     <tr> 
      <td>6</td> 
     </tr> 
     <tr> 
      <td>7</td> 
     </tr> 
     <tr> 
      <td>8</td> 
     </tr> 
     <tr> 
      <td>9</td> 
     </tr> 
     <tr> 
      <td>10</td> 
     </tr> 
    </table> 

在上面的例子中,我想根據id「CurrentId」然後我想前往旁邊的ID的所有的TR並獲得價值發現價值4。在上面的例子中,我剛剛提到了10,但實時我不知道會產生多少tr。所以我想獲得id旁邊的所有值。請任何人幫助我一樣。

回答

1

使用ID選擇器選擇行和下一個獲取next和nextAll獲取下一行/行。您也必須先爲行添加tds,因爲您無法直接將文本放入tr。

Live Demo

currentRow = $('#CurrentId') 
nextRow = $('#CurrentId').next() 
nextAllRows = $('#CurrentId').nextAll() 
0

這裏得到它在這個例子中的一種方式。

var vals = []; 

$('#CurrentId').nextAll().each(function(){ 
    vals.push($(this).children().html()); 
}); 

console.log(vals.join(",")); 

//output 
//5,6,7,8,9,10 

這裏有一個JsFiddle證明。

我更新了小提琴只獲得第-TD,通過爲孩子指定一個選擇:

vals.push($(this).children("td:first-child").html()); 

你可能不需要說;我會留給你的。

相關問題