2012-07-02 10 views
6

ボタンが選択されている場合、行のテーブルデータを取得する際に問題が発生しています。私は2つのボタンを承認し、拒否し、どのボタンのユーザーがクリックしたかに基づいて、クエリを使用してデータを取得したい。行番号を取得し、行データ以外のものを取得できます。私はIDとテスターを取得する必要があります。ここ その行のボタンをクリックして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数を得るために、私はJavaScriptで持っているものですが、私は私が必要なものを得るためにそれを使用する方法がわからないです

$(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

私はclosest()を使用してtrを取得しました。 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

関連する問題