2013-11-21 146 views
6

我看過很多關於我的問題的帖子,但是其中有一篇適合我。如何使用jQuery獲取html表格中的單元格值

我有這個html表,我會得到單元格(th)「索引」下的值。 如何我可以使用jQuery通過本次相對TD值使這個

<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

如何讓jQuery的https://codepedia.info/jquery-get-table-cell-td-value-div/ –

回答

12

我認爲這將幫助你

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

謝謝,它工作正常 – loi219

2

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 

,如果你只想要個值做到這一點

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

試試這個:

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

Example fiddle

+0

+1 TD值理解問題,我不明白這一點詳細的文章 –

+0

謝謝你的回答。 它運行良好,但我會有索引單元格下的值。 – loi219

2

要獲得<th>標籤本身的內容:

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

遍歷隨後<td>標籤,並得到他們的價值觀:

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

或者撥弄着它自己:http://jsfiddle.net/chaiml/p2uNv/4/

0

嘗試下面的代碼。

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

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

    alert(a); 
}); 
相關問題