我一直有这个问题,我不能完全通过其他给定的解决方案来解决。
我正在尝试获取表中每一行的特定值。这个问题来自一个相当没有格式化的表,所以我必须根据它们的索引或位置来获取单元格的值。
这是有问题的表格:
<table width="100%">
<thead>
<tr>
<td>Item Description</td>
<td>Colour</td>
<td>Size</td>
<td class="text-right">Price</td>
<td class="text-right">Qty</td>
<td class="text-right">Total</td>
</tr>
</thead>
<tbody>
<tr id="row_24551">
<td width="40%">Item 1</td>
<td>Ivory</td>
<td>10</td>
<td class="text-right">$ 19.00</td>
<td class="text-right">1</td>
<td class="text-right">$ 19.00</td>
</tr>
<tr id="row_24550">
<td width="40%">Item 2</td>
<td>Grey Marle</td>
<td>10</td>
<td class="text-right">$ 18.95</td>
<td class="text-right">1</td>
<td class="text-right">$ 18.95</td>
</tr>
</tbody>
</table>
我一直使用以下命令获取行值:
作为所需信息的var table = $('tr[id^="row"]')
位于具有ID的行中。
可以输出每个单元格的内容
var table = $('tr[id^="row"]');
table.each(function(){
console.log('row');
$.each(this.cells, function(){
console.log($(this).text());
});
});
我被卡住的地方是,我只想要单元格0,2,4,5,而且我不确定如何通过.each函数进行选择。
For循环对我来说更有意义,但我还不能通过行获得单元的迭代:
var table = $('tr[id^="row"]');
for(i = 0; i < table.length; i++) {
console.log("row");
}
我也试过这个
var table = $('tr[id^="row"]');
for(var i = 0, row; row = table[i]; i++) {
console.log("row");
for (var j = 0, col; col = row[j]; j++) {
console.log("cell");
}
}
但是单元格的值似乎不能通过。
最终希望将所需的每个单元格值( 0,2,4,5 )推送到一个product数组中,该数组将映射{name,size,quantity,price}。不过,如果能帮上忙,我会非常感激的!
https://stackoverflow.com/questions/48621585
复制相似问题