admin管理员组文章数量:1356294
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 :(
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 :(
Share Improve this question asked Apr 14, 2011 at 13:18 Michael MaoMichael Mao 10.2k23 gold badges78 silver badges92 bronze badges 2-
3
You variable "tmp" is not really being used as an array, so you should declare it as
var tmp = { };
- and don't forget to declare all your local variables with thevar
keyword!!! – Pointy Commented Apr 14, 2011 at 13:23 - 1 Might be better to add value= attributes to the inputs in the html instead of relying on the code count elements and assign values. – wdm Commented Apr 14, 2011 at 13:29
2 Answers
Reset to default 7You don't have a td
with more than one input
, so nth-child(2)
won't find anything.
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.
$(tr).find('td input:eq(2)').is(':checked');
本文标签: javascriptjQueryWhy first and last work but not nthchild(2) in my codeStack Overflow
版权声明:本文标题:javascript - jQuery - Why :first and :last work but not :nth-child(2) in my code? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743989653a2571900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论