我正在尝试使用 jquery 获取生成的表并从中创建一个对象。我已经查找了 示例,但是当我尝试实现时遇到了一些奇怪的行为。鉴于我的表的这个简化版本(通过 Spring MVC 生成):
<table id="notices">
<thead>
<tr>
<td class="columnheader">Order</td>
<td class="columnheader" style="display: none;">ID</td>
<td class="columnheader">Title</td>
</tr>
</thead>
<tbody>
<tr>
<td class="formlabel"><input class="fields" size="2" type="text" value="3"></td>
<td class="formlabel" style="display: none;">JP-L2913666442781178567X</td>
<td class="formlabel"><a href="javascript:void(0);" class="editNoticeOpen">*Notice1</a></td>
</tr>
<tr>
<td class="formlabel"><input class="fields" size="2" type="text" value="2"></td>
<td class="formlabel" style="display: none;">JP-L2913666442760937100X</td>
<td class="formlabel"><a href="javascript:void(0);" class="editNoticeOpen">Quiz Notice - Formative</a></td>
</tr>
</tbody>
</table>
以及我当前脚本的片段:
var noticeMap = $('#notices tbody tr').map(function() {
var $row = $(this);
return {
sequence: $row.find(':nth-child(1)').text(),
noticeUID: $row.find(':nth-child(2)').text()
};
});
当我 de[fire]bug 时,noticeMap 看起来像这样:
Object { sequence="*Notice1", noticeUID="JP-L2913666442781178567X"},
Object { sequence="Quiz Notice - Formative", noticeUID="JP-L2913666442760937100X"}
不知何故:nth-child(1)
正在检索标题,第三个td
。我相信这与检索输入的值有关,但我不确定从这里去哪里。可能是因为输入字段在我指定的 td 子项中,所以它不被视为直系后代,所以没有检索到正确的文本?对我来说似乎很奇怪,然后它会跳到第三个 td。唉,我还在用jquery学习,谦虚地请求任何想法和指导。
谢谢!