Jquery - 为所有具有类的 tr 选择 td 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5990089/
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
Jquery - Select td value for all the tr with a class
提问by Lamps
I have a table
我有一张桌子
<table>
<tr class="PO1"></tr>
<tr class="PO2"></tr>
<tr class="PO3"></tr>
</table>
How can I loop through all tr with class "PO1"
and get the value of each 'td'
value?
如何使用 class 遍历所有 tr"PO1"
并获取每个'td'
值的值?
$("table#id tr .PO1").each(function(i)
{
// how to get the td values??
});
回答by jAndy
var values = $('table tr.PO1 td').map(function(_, td) {
return $(td).text();
}).get();
This would just create an array with the text contents from each td
. Probably a better idea to use a map/object instead:
这只会创建一个包含来自 each 的文本内容的数组td
。使用地图/对象可能是一个更好的主意:
var values = $('table tr.PO1 td').map(function(index, td) {
var ret = { };
ret[ index ] = $(td).text();
return ret;
}).get();
回答by wewals
The space before the .P01 is what's breaking your current code.
.P01 之前的空格是破坏您当前代码的原因。
$("tr.PO1 td").each(function(i){
$(this).text()
});
回答by Grooveek
notice : I removed a space before .PO1 because your tr has class P01
注意:我删除了 .PO1 之前的一个空格,因为你的 tr 有类 P01
$("table#id tr.PO1").each(function(i)
{
$(this).find("td").innerHTMl() //for example
});
回答by Stijn Janssen
$("table#id tr.PO1").each(function(i)
{
i.children('td').each(function(tdEL) {
// tdEl.val();
});
});
Notice the space I removed between tr and .PO1. In you case it will try to find each tr with an child having the class .PO1.
注意我删除了 tr 和 .PO1 之间的空格。在你的情况下,它会尝试找到每个 tr 和一个具有 .PO1 类的孩子。