我想获取所有行的记录,即使它有 2 或 3 列。
我想从 tbody
获取所有值。
我试过这段代码。但它不起作用
这是代码
$("#btnSearch").click(function() {
$("table > tbody > tr").each(function() {
alert($("#FieldNameID").text() + " " + $("#OperatorID").text());
});
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnSearch">Search</button>
<table class="table table-hover " id="queryTable">
<thead>
<tr>
<th>Field Name</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td class="FieldNameID">tq.StoreID</td>
<td class="OperatorID"> IN('1001')</td>
</tr>
<tr>
<td class="FieldNameID">AND item.SubDescription1</td>
<td class="OperatorID"> IN('215')</td>
</tr>
<tr>
<td class="FieldNameID">AND dept.Name</td>
<td class="OperatorID"> IN('Rent Department')</td>
</tr>
<tr>
<td class="FieldNameID">AND sup.SupplierName</td>
<td class="OperatorID"> IN('MOHAMMED ABDUL RAHMANمحمد عبد')</td>
</tr>
<tr>
<td class="FieldNameID">AND sup.SupplierName</td>
<td class="OperatorID"> IN('MACRONA FACTORYمحمد يسلم ناصر')</td>
</tr>
</tbody>
</table>
原文由 mohamed hafil 发布,翻译遵循 CC BY-SA 4.0 许可协议
FieldNameID
是td
DOM 元素的类,因此您必须将选择器更改为$(".FieldNameID")
。另一种解决方案是使用
.eq()
方法,将匹配元素的集合减少到指定索引处的元素。另一种方法是使用
.children
方法,该方法获取匹配元素集中每个元素的子元素,可选地由选择器过滤。