2010-06-11 6 views
2

J'ai utilisé ce code pour obtenir la valeur de colonne particulière en utilisant jquery ..Comment obtenir la valeur de colonne particulière en utilisant jquery

$("#Grid td:first-child").click(function() { 
       var resultArray = $(this).closest('tr').find('td').text(); 
        alert(resultArray); 
      }); 

Comment puis-je obtenir la valeur de colonne particulière? C'est-à-dire, j'ai besoin de la grille 4ème et 5ème colonne valeur?

+0

en double de http://stackoverflow.com/questions/3019938/how-to-get-the-jquery-grid-entire-row-values-on-click/3020032#3020032 – griegs

+0

http: // stackoverflow.com/questions/3015598/jquery-column-values-on-click/3015687 – Reigel

Répondre

4

Utilisez le :eq sélecteur:

$("#Grid td:first-child").click(function() { 
    var value = $(this).closest('tr').find('td:eq(2)').text(); // for third column 
    alert(value); 

    var value = $(this).closest('tr').find('td:eq(3)').text(); // for fourth column 
    alert(value); 
}); 

qui alerte la valeur de la 3e et 4e TD/colonne lors de la première td de l'élément avec id Grid (td:first-child) est cliqué.

Cependant, si vous voulez un tableau de valeurs de TDs, utilisez les map et get méthodes comme celle-ci:

$("#Grid td:first-child").click(function() { 
    var value_array = $(this).closest('tr').find('td').map(function(){ 
    return $(this).text(); 
    }).get(); 
}); 

Maintenant value_array volonté contient du texte pour TDs trouvés par exemple:

value_array[0] // first 
value_array[1] // second 
value_array[2] // third 
1
$('#Grid td:first-child').click(function(){ 
    var resultArray = $(this).closest('tr').find('td').map(function(){ 
     return $(this).text(); 
    }).get(); 

    alert(resultArray[2]); // third 
    alert(resultArray[3]); // fourth.. 

}); 

ou

$('#Grid td:first-child').click(function(){ 
    var resultArray = $(this).closest('tr') 
     //  third  fourth 
     .find('td:eq(2), td:eq(3)').map(function(){ 
      return $(this).text(); 
     }).get(); 

    alert(resultArray[0]); 
    alert(resultArray[1]); 

}); 
Questions connexes