6

ボタンが選択されている場合、行のテーブルデータを取得する際に問題が発生します。承認と拒否の2つのボタンがあり、ユーザーがクリックしたボタンに基づいて、クエリを使用してデータを取得します。行データだけでなく、行番号などを取得できます。IDとテスターを取得する必要があります。

これが私が持っているものです

<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(),

        });


});
4

4 に答える 4

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

于 2012-07-02T23:55:25.113 に答える
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);
    });
});​

フィドル

于 2012-07-03T00:06:00.520 に答える
3

私はclosest()を取得しtr、そこから降りるために使用します。

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

また、これは不要だと思います。あなたの例では、次のようになります$(this)

$(this).parent().children().index($(this)) // === $(this)
于 2012-07-02T23:56:07.723 に答える
2
$('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);
});​
于 2012-07-03T00:00:46.917 に答える