2016-09-27 18 views
0

我使用一個jQuery移動錶行,當我放開行,我想獲得它具有類group_headingJquery - 按類別取得最近的TR ID?

最近的TR的ID當我移動行被釋放,我得到它的使用ID:

var row_id = $(row).prop('id'); 
console.log (row_id) 

導致sprint_145975

如何獲得最近的TR來sprint_145975與類group_heading的ID?

這是表格佈局。

<tr id="present_sprints" class="group_heading nodrag"> 
    <td colspan="4">LIST1</td> 
</tr> 
<tr id="sprint_145664" style="cursor: move;"> 
    <td>First round - in sprint</td> 
    <td>2012-09-19</td> 
    <td>2012-09-27</td> 
</tr> 
<tr id="sprint_145975" class="toggler_row" style="cursor: move;"> 
    <td>Third round</td> 
    <td>2012-10-04</td> 
    <td>2012-10-11</td> 
</tr> 
<tr id="sprint_145966" class="toggler_row" style="cursor: move;"> 
    <td>Release prep</td> 
    <td>2012-10-20</td> 
    <td>2012-10-27</td> 
</tr><tr id="sprint_145665" class="toggler_row" style="cursor: move;"> 
    <td>Second round</td> 
    <td>2012-09-28</td> 
    <td>2012-10-04</td> 
</tr> 

<tr id="future_sprints" class="group_heading nodrag"> 
    <td colspan="4">Future Sprints</td> 
</tr> 
<tr id="sprint_145964" class="toggler_row" style="cursor: move;"> 
    <td>Fifth run</td> 
    <td>2012-10-27</td> 
    <td>2012-11-03</td> 
</tr> 
<tr id="sprint_145965" class="toggler_row" style="cursor: move;"> 
    <td>Fourth round</td> 
    <td><input type="text" value="2012-10-13" <="" td=""> 
    </td><td>2012-10-20</td> 
</tr> 

對於上面我的例子將期待得到ID =「present_sprints」

如果被移動行的ID是sprint_145965,我期望在最近的TR的ID with class group_heading爲future_sprints

我該怎麼做?

我已經試過:

$('#sprint_145975').closest('.group_heading).prop('id); 
$('#sprint_145975').closest('tr.group_heading).prop('id); 

但是,這並不會工作。

感謝

+1

通過最近的你是什麼意思一個或下一個? – guradio

回答

2

您正在尋找.prevAll(),而不是.closest()。您可以使用.eq()僅獲取第一個。你應該使用.attr()而不是.prop()

var heading = $('#sprint_145975').prevAll('.group_heading').eq(0); 
 
console.log(heading.attr('id')); 
 

 
heading = $('#sprint_145964').prevAll('.group_heading').eq(0); 
 
console.log(heading.attr('id'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<table> 
 
    <tr id="present_sprints" class="group_heading"></tr> 
 
    <tr id="sprint_145975"></tr> 
 
    <tr id="future_sprints" class="group_heading"></tr> 
 
    <tr id="sprint_145964"></tr> 
 
</table>

+0

完美 - 謝謝,完全按照我的需要。 – MacMan

+0

不客氣,@MacMan – eisbehr