2016-09-27 2 views
0

나는 테이블 행을 이동하기 위해 jQuery를 사용하고 있는데 내가 행을 놓을 때 나는 이동하고있어 행이 내가 얻을 출시되면 I 클래스 group_headingJquery - 클래스별로 가장 가까운 TR ID를 받으시겠습니까?

이있는 가장 가까운 TR의 ID를 좀하고 싶습니다 sprint_145975

결과

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

가 어떻게 클래스 group_headingsprint_145975에 TR 가까운의 ID를받을 수 있나요 : 그것은 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를 기대할 수 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()을 사용하면 첫 번째 값만 가져올 수 있습니다. 그리고 .prop() 대신 .attr()을 사용해야합니다. 가장 가까운에 의해

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

관련 문제