javascript 获取 jquery dataTable 中选中复选框的行 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the row ids for the checked checkboxes in jquery dataTable?
提问by M Sach
I have a DataTable with one of the column is checkbox. I can select multiple checkboxes. now on click of button i want to get row ids of selected checkboxes. Once the ajax operation is done on click of button, i want to select the checkboxes again for the above row ids. Basically how to get the checked row ids and then check the checkboxes again?
我有一个 DataTable,其中一列是复选框。我可以选择多个复选框。现在单击按钮,我想获取所选复选框的行 ID。单击按钮完成 ajax 操作后,我想再次选择上述行 ID 的复选框。基本上如何获取选中的行 ID,然后再次选中复选框?
回答by rdiazv
Assuming each checkbox has the RowId as his value attribute.
假设每个复选框都有 RowId 作为他的值属性。
Get all the selected checkboxes:
获取所有选中的复选框:
var selectedIds = [];
$(":checked").each(function() {
selectedIds.push($(this).val());
});
Re-check the checkboxes:
重新选中复选框:
$.each(selectedIds, function(index, id) {
$(":checkbox[value='" + id + "']").prop("checked", true);
});
回答by Swarne27
If I understood your question correctly, is this what you are looking for,
如果我正确理解你的问题,这就是你要找的吗?
JQuery code
查询代码
$(document).ready(function() {
$('#btn').click(function(){
var dataArr = [];
$('input:checked').each(function(){
alert($(this).closest('tr[id]').attr('id'));// just to see the rowid's
dataArr.push($(this).closest('tr[id]').attr('id')); // insert rowid's to array
});
// send data to back-end via ajax
$.ajax({
type : "POST",
url : 'server.php',
data : "content="+dataArr,
success: function(data) {
alert(data);// alert the data from the server
},
error : function() {
}
});
});
});
html code
html代码
<table border="2" cellspacing="2" cellpadding="2">
<tr id="row1">
<td><input id="checkbox1" name="checkbox1" type="checkbox" value="1" /></td>
<td> </td>
<td> </td>
</tr>
<tr id="row2">
<td><input id="checkbox2" name="checkbox2" type="checkbox" value="2" /></td>
<td> </td>
<td> </td>
</tr>
<tr id="row3">
<td><input id="checkbox3" name="checkbox3" type="checkbox" value="3" /></td>
<td> </td>
<td> </td>
</tr>
</table>
<input id="btn" name="btn" type="button" value="CLICK" />