jQuery - 为什么 :first 和 :last 在我的代码中有效,但 :nth-child(2) 无效?
请考虑以下 html 片段及其对应的 jQuery 版本 1.3.2 代码:
<tr>
<td>id</td>
<td><input type='checkbox' /></td>
<td><input type='checkbox' /></td>
<td><input type='checkbox' /></td>
<td><select>
<option value='0'>0</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select></td>
</tr>
jQuery function:
function submitRoutesForm()
{
data = new Array();
$('#routes_table option:selected').each(function()
{
qty = $(this).val();
if( qty != 0)
{
tmp = new Array();
tr = $(this).closest('tr');
tmp['route_id'] = tr.find('td:first').html();
tmp['qty'] = qty;
tmp['isChild'] = $(tr).find('td input:first').is(':checked');
tmp['isInvalid'] = $(tr).find('td input:nth-child(2)').is(':checked');
tmp['isSpecialDiet'] = $(tr).find('td input:last').is(':checked');
data.push(tmp);
console.log(tmp);
}
});
return false;
}
我可以确认一切正常,期望第二个复选框的结果始终返回“false”。看来我的选择器 :nth-child(2) 由于某种原因不起作用...
提前非常感谢,我被这个问题困扰了一段时间:(
Please consider the following html snippet and its corresponding code with jQuery version 1.3.2:
<tr>
<td>id</td>
<td><input type='checkbox' /></td>
<td><input type='checkbox' /></td>
<td><input type='checkbox' /></td>
<td><select>
<option value='0'>0</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select></td>
</tr>
jQuery function:
function submitRoutesForm()
{
data = new Array();
$('#routes_table option:selected').each(function()
{
qty = $(this).val();
if( qty != 0)
{
tmp = new Array();
tr = $(this).closest('tr');
tmp['route_id'] = tr.find('td:first').html();
tmp['qty'] = qty;
tmp['isChild'] = $(tr).find('td input:first').is(':checked');
tmp['isInvalid'] = $(tr).find('td input:nth-child(2)').is(':checked');
tmp['isSpecialDiet'] = $(tr).find('td input:last').is(':checked');
data.push(tmp);
console.log(tmp);
}
});
return false;
}
I could confirm that everything works expect the result for the second checkbox is always returning "false". It seems my selector :nth-child(2) doesn't work for some reason...
Many thanks in advance, I am stuck with this for a while :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
td
没有多个输入
,因此nth-child(2)
不会找到任何内容。You don't have a
td
with more than oneinput
, sonth-child(2)
won't find anything.nth-child 将寻找 td 元素的子元素,并且每个元素只有 1 个子元素。您应该在 td 上使用 :eq(2)。这将为您提供匹配结果集的索引,而不是某个子项。
nth-child would be looking for a child of the td element and each one only has 1 child. You should use :eq(2) on the td. That will give you the index of the matching result set instead of a certain child.