2013-11-21 22 views
6

Widziałem wiele postów na temat mojego problemu, ale jeden z nich dla mnie.Jak uzyskać wartość komórek w tabeli html za pomocą JQuery

Mam tabelę html, chciałbym uzyskać wartości pod komórką (th) "Indeks". jaki sposób można użyć jQuery do podejmowania tego

<table id="htmlTable"> 
    <caption>Informations des hotspots</caption> 
    <thead> 
     <tr> 
      <th>Index</th> 
      <th>Nom du hotspot</th> 
      <th>Image du hotspot</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr id="0"> 
      <td>0</td> 
      <td>Hotspot Fribourg Centre</td> 
      <td>../images/logos_hotspot/logo_wifi_centre.png</td> 
      <td> 
       <input type="button" value="supprimer" /> 
      </td> 
     </tr> 
     <tr id="1"> 
      <td>1</td> 
      <td>Hotspot Avry Centre</td> 
      <td>../images/logos_hotspot/logo_wifi_avry.png</td> 
      <td> 
       <input type="button" value="supprimer" /> 
      </td> 
     </tr> 
    </tbody> 
</table> 
+0

szczegółowy artykuł o tym, jak uzyskać wartość td w jQuery https://codepedia.info/jquery-get-table-cell-td-value-div/ –

Odpowiedz

12

Myślę, że to ci pomoże

var MyRows = $('table#htmlTable').find('tbody').find('tr'); 
for (var i = 0; i < MyRows.length; i++) { 
var MyIndexValue = $(MyRows[i]).find('td:eq(0)').html(); 
} 
+0

Dzięki, działa dobrze – loi219

2

przez to th względnej wartości td przyjść

var tharr=[]; 
    $("#htmlTable").find("tbody tr").each(function(){ 
    tharr.push($(this).find("td:eq(0)").text()); 

    }); 

alert(tharr.join(",")); //by this you get 0,1 

a jeśli chcesz tylko th wartości to zrobić

$('#htmlTable tr th:first').text(); 
2

Spróbuj tego:

var text = $('#htmlTable tr th:first').text(); // = "Index" 

Example fiddle

+0

+1 dla zrozumienia pytanie ja nie rozumiem –

+0

Dziękuję za odpowiedź. Działa dobrze, ale chciałbym mieć wartości w komórce indeksu. – loi219

2

Aby uzyskać zawartość znacznika samego <th>:

$('#htmlTable th:first').html() 

przemierzać kolejne <td> tagi i uzyskać ich wartości:

$('#htmlTable tr:gt(0)').each(function(){ 
    console.log($('td:first', $(this)).html()); 
}); 

lub skrzypce z nim sobie: http://jsfiddle.net/chaiml/p2uNv/4/

0

Wypróbuj poniższy kod.

$(document).on("click", "[id*=tableId] tr", function() { 

    var a = $(this).find("td").eq(3).children().html(); 

    alert(a); 
}); 
Powiązane problemy