[英]Get column in a row in a table if that row has a selected checkbox
我有一張像下面的桌子
<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。
我已經發現的是這個 ,但是我無法得到它的幫助,因為我不想在我的行中放置一個按鈕(可以通過復選框進行多個選擇)。
您可以使用在單擊按鈕時運行的以下功能:
$(":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
});
$("#adminTable").find("td").filter(function()
{
return $(this).find("input[type=checkbox]:checked").length > 0;
});
請記住,您的html中有TR標簽,沒有結束標簽,並且位置不合適。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.