如果该行具有选中的复选框,则在表中连续获取列

时间:2013-11-05 00:15:37

标签: javascript jquery html-table

我有一张如下表格

<table id="adminTable">
<thead>
<tr>
<th></th>
<th>Username</th>
<th>Email</th>
<tr>
</thead>
<tbody>
<tr><td><input type='checkbox' name='selected_users[]' value='User1'/></td><td>User1</td><td>mail1@mail.com</td></tr>
<tr><td><input type='checkbox' name='selected_users[]' value='User2'/></td><td>User2</td><td>mail2@mail.com</td></tr>
...
</tbody>
</table>

我在下面有一个按钮,可以调用javascript函数。 然后,此功能应获取通过其复选框选择的用户的所有电子邮件...

我是Javascript和jQuery的新手,我不知道如何实现这个... 但我可以使用普通的Javascript或使用jQuery ...

我已经找到的是this,但我无法帮助我,因为我不想在我的行中有一个按钮(通过复选框可以进行多项选择)。

2 个答案:

答案 0 :(得分:1)

您可以使用以下按钮单击时运行的功能:

$(":button").click(function() {
    var emails = $("input[name=selected_users[]]:checked").map(function() {
        return $(this).closest("td").next("td").text();
    }).get();

    console.log(emails); //array of emails that were checked
});

答案 1 :(得分:0)

$("#adminTable").find("td").filter(function() 
{ 
    return $(this).find("input[type=checkbox]:checked").length > 0;
}); 

请记住,你的html中有TR标签没有结束标签且不合适。