如何使用 JQuery 在 html 表中获取单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20116547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a cells value in html table with JQuery
提问by loi219
I seen a lot of post talk about my problem but any of one work for me.
我看到很多帖子都在谈论我的问题,但其中任何一个都对我有用。
I've this html table, I would to get the values under the cell (th) "Index". How can I use jQuery for making this:
我有这个 html 表,我想获取单元格(th)“索引”下的值。我如何使用 jQuery 来实现这一点:
<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>
回答by Mohamed Rashid.P
I Think This Will Help You
我认为这会帮助你
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();
}
回答by Rituraj ratan
by this th relative td value come
由这个相对 td 值来
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
and if you want only th value do this
如果你只想要 th 值,请执行此操作
$('#htmlTable tr th:first').text();
回答by Rory McCrossan
回答by Chaim Leichman
To get the content of the <th>
tag itself:
要获取<th>
标签本身的内容:
$('#htmlTable th:first').html()
To traverse the subsequent <td>
tags and get their values:
要遍历后续<td>
标签并获取它们的值:
$('#htmlTable tr:gt(0)').each(function(){
console.log($('td:first', $(this)).html());
});
Or fiddle with it yourself: http://jsfiddle.net/chaiml/p2uNv/4/
或者自己摆弄:http: //jsfiddle.net/chaiml/p2uNv/4/
回答by Son Cao
Try the code below.
试试下面的代码。
$(document).on("click", "[id*=tableId] tr", function () {
var a = $(this).find("td").eq(3).children().html();
alert(a);
});