开发者

jQuery - Why :first and :last work but not :nth-child(2) in my code?

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 :(


You 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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜