2009-12-22 6 views

Répondre

1

Essayez ceci:

$content = $('#your_cell_id_here').html(); 

if($content == '') 
{ 
    // yes it is empty 
} 
else 
{ 
    // no it is not empty 
} 
5

Vous pouvez utiliser des sélecteurs CSS avec la fonction $ pour obtenir une référence à l'élément de la cellule, puis utilisez la fonction html pour voir si elle a tout contenu. Par exemple, si la cellule a un ID "foo":

if ($("#foo").html()) { 
    // The cell has stuff in it 
} 
else { 
    // The cell is empty 
} 
+0

Merci. Mais tout cela ne fonctionne que dans IE et non dans Firefox. Je l'ai vérifié maintenant. – Yaswanth

+2

Testé et fonctionne dans IE7, FF3.5, Chrome3, Safari4 et Opera10. Page de test: http://pastie.org/753004 Sortie attendue: "foo a le contenu/la barre est vide" Assurez-vous d'autoriser les espaces; vous voudrez peut-être couper la chaîne renvoyée par 'html'. –

0

Vous pouvez ajouter des classes CSS à vos table et ses lignes et de colonnes, puis utilisez jquery selectors pour obtenir l'élément de table:

if (!($('#mytable tr.row-i td.column-j').html())) { alert("empty"); } 
10

Que voulez-vous dire par vide.

//cell maycontain new lines,spaces,&npsp;,... but no real text or other element 
$("cellselector").text().trim()==""; 

ou

//cell has no child elements at all not even text e.g. <td></td> 
$("cellselector:empty") 
0

Vous pouvez utiliser le trechnique que j'ai posté ici

http://www.keithrull.com/2010/06/09/HowToChangeTableCellColorDependingOnItsValueUsingJQuery.aspx

Vous pouvez l'utiliser si vous connaissez déjà l'ID de la cellule que vous voulez vérifier

$valueOfCell = $('#yourcellid').html(); 

ou vous pouvez itérer sur les cellules de la table

$("#yourtablename tr:not(:first)").each(function() {       
//get the value of the table cell located    
//in the third column of the current row    
var valueOfCell = $(this).find("td:nth-child(3)").html();       
//check if its greater than zero    
if (valueOfCell == ''){     
    //place action here 
}    
else{     
    //place action here 
}   

});

Questions connexes