【发布时间】:2013-07-27 21:35:53
【问题描述】:
这是 HTML:
<table id="tblTestAttributes">
<thead>
<tr> <th>Head 1</th> <th>Head 2</th> </tr>
</thead>
<tbody>
<tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr>
<tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr>
<tr> <td id="txtDesc">Item 1</td> <td id="ddlFreq">Assume a DropDownList Here</td> </tr>
</tbody>
</table>
这是获取每一行值的javascript:
var frequencies = [];
if ($('#tblTestAttributes').length) {
$('#tblTestAttributes tr').each(function () {
var t = $(this).find('td[id^="txtDesc"]').text() + ";" + $(this).find('[id^="ddlFreq"] option:selected').val();
alert(t);
frequencies.push(t);
});
}
我想避免第一行,其中包含 th 元素,这些元素只是显示标题,不包含任何数据。
所以我把选择器改成了这样:
#tblTestAttributes tr:not(:first-child)
这也跳过了第二个tr。这里发生了什么?
【问题讨论】:
-
ID 必须是唯一的。改用类。
-
已编辑以在 HTML sn-p 中包含 thead 和 tbody。
-
使用 ID 而不是类是正确的。直到你提到我才注意到。 HTML(在我们的项目中)再次完全掌握在 UI 设计师手中,所以我需要将此更改推送给他们。谢谢。
标签: javascript jquery