2012-07-02 14 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> 

यहाँ टीआर और टीडी संख्या प्राप्त करने के लिए अपने जावास्क्रिप्ट है क्या है है, लेकिन मैं यह कैसे उपयोग करने के लिए मैं क्या जरूरत है पाने के लिए यकीन नहीं है

$(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() का उपयोग करूंगा और वहां से उतरो।

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

संबंधित मुद्दे