How to get the list of items that have a particula

2019-08-29 01:19发布

I have table where each row starts with a "checkbox", I'd like to get all the TR that contains a checkbox and it is check.

Something like this:

$('#' + tableId + ' > tbody > tr:contains(td > input[type=checkbox]:checked').each(function (i, item) {

        alert('delete ' + $(item).data('row-id'));

    });

That code doesn't work, it gets stuck. Probably because the "contains" selector is only for text. How could I do it?

Regards.

3条回答
太酷不给撩
2楼-- · 2019-08-29 01:57

You could just grab the checkboxes and then find their closest tr:

$('#' + tableId + ' > tbody > tr > td > input[type=checkbox]:checked').closest("tr")

Note, you're also missing ) to close the contains.

查看更多
乱世女痞
3楼-- · 2019-08-29 02:05

You don't need to :contains selector.

If you want it to be anywhere inside the <tr> (even inside nested elements)

$('#' + tableId + ' > tbody > tr td > input[type=checkbox]:checked').each(function (i, item) {

If you want it directly inside of it

$('#' + tableId + ' > tbody > tr > td > input[type=checkbox]:checked').each(function (i, item) {
查看更多
可以哭但决不认输i
4楼-- · 2019-08-29 02:06

Use jQuery's has:

$('#' + tableId + ' > tbody > tr:has(td > input[type=checkbox]:checked)').each(function (i, item) {

  alert('delete ' + $(item).data('row-id'));

});

For better performance (see the jQuery API docs), use this:

$('#' + tableId + ' > tbody > tr')
.filter(':has(td > input:checkbox:checked)').each(function (i, item) {

  alert('delete ' + $(item).data('row-id'));

});

Since the has query cannot be run through querySelectorAll(), there's no need to use [type=checkbox].

查看更多
登录 后发表回答