Jquery 如何获取表中以数组形式选择的复选框的Id
我有一张如下所示的表格Jquery 如何获取表中以数组形式选择的复选框的Id,jquery,Jquery,我有一张如下所示的表格 <table id="sample_1" aria-describedby="sample_1_info"> <thead> <tr> <th><input type="checkbox" id="selecctall"></th> <th>Status</th> </tr> </thea
<table id="sample_1" aria-describedby="sample_1_info">
<thead>
<tr>
<th><input type="checkbox" id="selecctall"></th>
<th>Status</th>
</tr>
</thead>
<tbody class="odd gradeX">
<tr>
<td width="5%"><input type="checkbox" class="checkbox1" id="1"></td>
<td width="37%">Chips And Chocolates,Bummy Chips,Plain Salted,ytrytr,hyryr</td>
</tr>
<tr>
<td width="5%"><input type="checkbox" class="checkbox1" id="2"></td>
<td width="37%">Chips And Chocolates</td>
</tr>
</tbody>
</table>
<input type="button" class="btn blue" value="Delete" id="deletebtn">
$('#selecctall').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
你可以试试这个。请参阅控制台
$('#deletebtn').click(function() {
var array = $('#sample_1 input:checked');
var ids = new Array();
$.each(array, function(idx, obj) {
ids.push($(obj).attr('id'));
});
console.log(ids);
});
使用以下命令:
else {
var ids = [];
$.each($checked, function(i,e){
console.log(e);
if ($(e).attr("id") != 'selecctall'){
ids.push($(e).attr("id"))
}
});
我建议不要费心获取选中元素的id,只需使用节点:
$("#deletebtn").click(function () {
// retrieve a jQuery collection of the checked <input>s from the <tbody>
// which means we ignore the 'select-all' checkbox):
var $checked = $('#sample_1 tbody input[type="checkbox"]:checked');
// if we have any checked elements:
if ($checked.length) {
// we find the closest <tr> elements to those checked <inputs>, and
// remove them:
$checked.closest('tr').remove();
} else {
// otherwise, we alert:
alert("You need to check at least one checkbox.");
}
});
参考资料:
.
.
试试这个代码
答案更新:您必须使用:不排除第一个复选框。问题是什么?如果选中该复选框,我如何获取其id?其工作状态的可能副本,我是否可以以数组的形式获取结果?我如何从结果中删除selectall?我如何从结果中删除selectall?–@PreethiJain-请参阅更新的答案。它将排除SELECTALL复选框
$("#deletebtn").click(function () {
// retrieve a jQuery collection of the checked <input>s from the <tbody>
// which means we ignore the 'select-all' checkbox):
var $checked = $('#sample_1 tbody input[type="checkbox"]:checked');
// if we have any checked elements:
if ($checked.length) {
// we find the closest <tr> elements to those checked <inputs>, and
// remove them:
$checked.closest('tr').remove();
} else {
// otherwise, we alert:
alert("You need to check at least one checkbox.");
}
});
var chkIds = $('#sample_1 :checkbox:checked:not(#selecctall)').map(function(i,n) {
return $(n).attr('id');
}).get().join(',');
alert(chkIds);