Jquery .每个:选中的选择器工作不正常

Jquery .每个:选中的选择器工作不正常,jquery,Jquery,以下是HTML: $('#deleteMessage').click(function () { $.each('input:checked', function () { var tr = $(this).closest('tr'); alert(tr.id); }); }); 我需要这个循环通过每个复选框,不包括。然后,我需要它来找到复选框所在的位置,并获取该id。我似乎无法使其正常工作。对于此场景,您没有使用正确版本的$。每个。试试这个:

以下是HTML:

$('#deleteMessage').click(function () {
    $.each('input:checked', function () {
        var tr = $(this).closest('tr');
        alert(tr.id);
    });
});

我需要这个循环通过每个复选框,不包括。然后,我需要它来找到复选框所在的位置,并获取该id。我似乎无法使其正常工作。

对于此场景,您没有使用正确版本的$。每个。试试这个:

<tr>
    <th width="40">&nbsp;&nbsp;&nbsp;<input type="checkbox" name="checkAll"></th>
    <th width="120">From</th>
    <th width="500">Subject</th>
    <th width="140">Date</th>
</tr>

<tr replytoid="3" messageid="2664" id="15" class="messageNew">
    <td>&nbsp;&nbsp;<input type="checkbox" name="checkAll"></td>
    <td>John Doe</td>
    <td>asd</td>
    <td>3/14/2012 1:09:04 PM</td>
</tr>

此外,tr是一个jquery对象,请使用[0]或.get0返回dom元素以获取其ID属性。

对于这种情况,您没有使用正确版本的$。每个。试试这个:

<tr>
    <th width="40">&nbsp;&nbsp;&nbsp;<input type="checkbox" name="checkAll"></th>
    <th width="120">From</th>
    <th width="500">Subject</th>
    <th width="140">Date</th>
</tr>

<tr replytoid="3" messageid="2664" id="15" class="messageNew">
    <td>&nbsp;&nbsp;<input type="checkbox" name="checkAll"></td>
    <td>John Doe</td>
    <td>asd</td>
    <td>3/14/2012 1:09:04 PM</td>
</tr>
此外,tr是jquery对象,请使用[0]或.get0返回dom元素以获取其ID属性。

您需要这样做

$('#deleteMessage').click(function () {
    $('input:checked').each(function () {
        var tr = $(this).closest('tr');
        alert(tr[0].id);
    });
});
你需要这样做

$('#deleteMessage').click(function () {
    $('input:checked').each(function () {
        var tr = $(this).closest('tr');
        alert(tr[0].id);
    });
});

问题是,对于您的代码:

$('input:checked').each(function(i, input) {
    var tr = $(input).closest('tr');
    alert(tr.attr('id'));
});
tr变量是一个jQuery对象,要获取id,需要使用attr方法:

此外,您使用每种方法都有问题。如果您修改以下内容:

$('#deleteMessage').click(function () {
                $.each('input:checked', function () {
                    var tr = $(this).closest('tr');
                    alert(tr.attr('id'));
                });
            });


它似乎可以工作,但由于您没有指定它不工作的方式,因此很难说清楚。

问题是,对于您的代码:

$('input:checked').each(function(i, input) {
    var tr = $(input).closest('tr');
    alert(tr.attr('id'));
});
tr变量是一个jQuery对象,要获取id,需要使用attr方法:

此外,您使用每种方法都有问题。如果您修改以下内容:

$('#deleteMessage').click(function () {
                $.each('input:checked', function () {
                    var tr = $(this).closest('tr');
                    alert(tr.attr('id'));
                });
            });

它似乎是有效的,但由于您没有指定它不起作用的方式,所以很难说清楚。

$。每个和。每个都是不同的。您希望使用.each来迭代jquery对象

$('#deleteMessage').click(function() {
    $('input:checked').each(function() {
        var tr = $(this).closest('tr');
        console.log(tr.attr('id'));
    });
});​
$。每个和。每个都是不同的。您希望使用.each来迭代jquery对象

$('#deleteMessage').click(function() {
    $('input:checked').each(function() {
        var tr = $(this).closest('tr');
        console.log(tr.attr('id'));
    });
});​
您可能想要tr.prop'id'而不是tr.id.

您可能想要tr.prop'id'而不是tr.id.

尝试:

$('input:checked').each(function(idx, el){
    // Stuff
});
循环检查每个选中的输入。将td添加到选择器将排除输入中的输入,请尝试:

$('input:checked').each(function(idx, el){
    // Stuff
});

循环检查每个选中的输入。将td添加到选择器将排除

中的输入如果您使用firebug,请删除警报并使用console.log您说的工作不正常是什么意思?@jon it会为当前20左右的每个复选框发送警报,而不仅仅是我为测试选中的2个复选框。它也不会提醒@Alp的id谢谢,我不知道该功能。@JamesWilson请记住,如果您使用的是具有控制台的版本,则在您打开控制台之前,console.log将无法在IE中工作。否则,它将抛出一个错误,可能不会执行代码。即使考虑到这一点,console.log也比使用警报要好得多,因为它不会像警报那样干扰代码,请清除警报并使用console.log。你说的工作不正常是什么意思?@jon it会针对当前20个左右的每个复选框发送警报,而不仅仅是我为测试检查的2个复选框。它也不会提醒@Alp的id谢谢,我不知道该功能。@JamesWilson请记住,如果您使用的是具有控制台的版本,则在您打开控制台之前,console.log将无法在IE中工作。否则,它将抛出一个错误,可能不会执行代码。即使考虑到这一点,console.log也比使用警报要好得多,因为它不会像警报那样干扰您的代码。谢谢,它工作得很好。现在我不知道该给谁答案。。。德里克或凯文布拉德我可以帮忙,但凯文先回答了。谢谢,效果很好。现在我不知道该给谁答案。。。derek或KevinBglad我可以帮忙,但Kevin确实先回答了。tr[0]不应该。id是tr[0]。attr'id'?@j08691否,因为TableCell元素没有attr方法。但是,tr.attr'id'和tr.prop'id'的工作方式与tr[0]相同。id.prop'id不应该是tr[0]。id是tr[0]。attr'id'?@j08691否,因为TableCell元素没有attr方法。但是,tr.attr'id'和tr.prop'id'的工作方式与tr[0].id相同。