2012-07-02 2 views
6

버튼을 선택하면 행에 테이블 데이터를 가져 오는 데 문제가 있습니다. 나는 두 개의 버튼을 승인하고 거부하고 어떤 버튼 사용자가 쿼리를 사용하여 데이터를 집어 넣을 것인지를 기반으로합니다. 행 번호를 가져 와서 행 데이터가 아닌 데이터를 가져올 수 있습니다. 나는 이드와 테스터를 얻을 필요가있다. 여기 해당 행의 버튼을 클릭하여 jquery를 사용하여 행 데이터 가져 오기

내가 여기
<table id="mytable" width="100%"> 
<thead> 
<tr> 
<th>ID</th> 
<th>Tester</th> 
<th>Date</th> 
<th>Approve</th> 
<th>Deny</th> 
</tr> 
</thead> 
<tbody> 
<tr class="test"> 
<td class="ids">11565 </td> 
<td class="tester">james</td> 
<td>2012-07-02 </td> 
<td><Button id="Approved" type="submit" >Approved</button> 
</td> 
<td><Button id="deny_0" type="submit" >Denied</button> 
</td> 
</tr> 
</tbody> 
</table> 

은 TR과 TD 번호를 내 자바 스크립트입니다 무엇을하지만 난 내가 필요한 것을 얻기 위해 그것을 사용하는 방법을 잘 모르겠습니다

$(document).ready(function() { 

    /*$('#cardsData .giftcardaccount_id').each(function(){ 

     alert($(this).html()); 
    }); */ 
    $('td').click(function(){ 
      var col = $(this).parent().children().index($(this)); 
      var row = $(this).parent().parent().children().index($(this).parent()); 
      alert('Row: ' + row + ', Column: ' + col); 
     // alert($tds.eq(0).text()); 
      console.log($("tr:eq(1)")); 
     // $("td:eq(0)", this).text(), 

     }); 


}); 

답변

7
$(document).ready(function(){ 
    $('#Approved').click(function(){ 
     var id = $(this).parent().siblings('.ids').text(); 
     var tester = $(this).parent().siblings('.tester').text(); 

     console.log(id); 
     console.log(tester); 
    }); 
});​ 

JSFiddle

2

나는 tr을 얻기 위해 closest()을 사용할 것입니다. n 거기서 내려왔다.

var tr = $('td').closest('tr') 

또한 나는 그 $(this) 것이이 예에서, 불필요한 생각 :

$(this).parent().children().index($(this)) // === $(this) 
1
$('table').on('click', 'button', function() { 
     var parentRow = $(this).parent().parent(); 
     var id = $('td.ids', parentRow).text(); 
     var tester = $('td.tester', parentRow).text(); 

    alert('id: ' + id + ', tester: ' + tester); 
});​ 
5
$(function(){ 
    $('button').on('click', function(){ 
     var tr = $(this).closest('tr'); 
     var id = tr.find('.ids').text(); 
     var tester = tr.find('.tester').text(); 
     alert('id: '+id+', tester: ' + tester); 
    }); 
});​ 

FIDDLE

관련 문제