2012-07-02 11 views
6

Ho problemi a ottenere i dati della tabella nella riga se è selezionato un pulsante. Ho due pulsanti per approvare e negare e in base a quale pulsante i clic dell'utente voglio catturare i dati utilizzando la query. può ottenere i numeri delle righe e roba solo non i dati delle righe. Devo avere l'id e il tester.recupera i dati della riga utilizzando jquery facendo clic sui pulsanti per quella riga

qui è quello che ho

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

qui è il mio javascript per ottenere il numero tr e td, ma non sono sicuro come usarlo per ottenere quello che mi serve

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

     }); 


}); 

risposta

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

Vorrei utilizzare closest() per ottenere il tr e il n scendiamo da lì.

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

Inoltre penso che questo non è necessario, nel tuo esempio che sarebbe $(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

Problemi correlati