2013-11-21 14 views
6

私の問題については多くのポストトークが見られましたが、私にとってはどちらかの作業があります。JQueryでhtmlテーブルのセル値を取得する方法

私はこのhtmlテーブルを持っています、私はセル(th) "インデックス"の下の値を取得するでしょう。 どのように私は

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 

来相対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

を作るためのjQueryを使用して、あなたが唯一の値番目たい場合は、この

$('#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); 
}); 
関連する問題