2010-12-23 1 views
7

J'ai 3 tables avec le même nom de classe table-sort. Je voudrais accéder à ceux table en .each() et compter le tr intérieur du tbody.

est-il $("this tbody tr").length?

$('.table-sort').each(function(index) { 
    var rowCount = $("this tbody tr").length; //not work , Could you please correct this? 

    var rowCount1 = $(this).find('tbody > tr').length; //this is working fine 
    alert(rowCount + '-' + rowCount1); 
}) 

Répondre

12

Voici le code

$('.table-sort').each(function(index) { 
    var rowCount = $("tbody tr", this).length; //will work now.. 

    var rowCount1 = $(this).find('tbody > tr').length; //this is working fine 
    alert(rowCount + '-' + rowCount1); 
}) 

Mais le second code que vous utilisez, qui fonctionne, devrait être suffisant ..


Vous pouvez également utiliser la inherent table properties of the table DOM object

$('.table-sort').each(function(index) { 
     var rowCount = this.rows.length; 
    }) 
+0

Merci Gaby, sa réponse étonnante, rapide. – Jahangir

+0

fonctionne génial !! –

Questions connexes