我将描述我想做的事情。
我有从数据库导入数据的表,对于每一行,我都有一个带有数据库ID(主键)的复选框。它看起来像这样。
cb =复选框
cb id = 1 |名字|内容| bla bla
等等。
我一次可以删除1行,但我想通过检查多个cheboxes删除多行,然后按删除按钮。当我检查其中的任何一个时,我正考虑将这些复选框id存入和数组,然后当我按下按钮删除所选复选框时再进行循环。
我的单个删除按钮代码如下所示(此按钮使用自己的ID进行删除,与同一行的复选框相同):
$('.del').click(function(){
var myid = $(this).attr('id');
$.post('newsdel.php', {delete_id:myid}, function(data) {
if(data == 'true') {
$('#'+myid).fadeOut();
var rowCount = $('#count tr').length;
if(rowCount < 3) {
window.location = '?news=1';
}
} else {
alert('Could not delete!');
}
});
});
答案 0 :(得分:1)
考虑到你的复选框有类复选框
function getCheckedRows() {
var checkedRows = [];
$('.checkbox').each(function() {
if($(this).is(':checked')) {
checkedRows.push($(this).attr('id'); //assuming id is the row id
}
});
return checkedRows;
}
$('.del').click(function(){
var rowIds = getCheckedRows();
for(var myid in rowIds) {
$.post('newsdel.php', {delete_id:myid}, function(data) {
if(data == 'true') {
$('#'+myid).remove();
var rowCount = $('#count tr').length;
if(rowCount < 3) {
window.location = '?news=1';
}
} else {
alert('Could not delete!');
}
});
}
});
答案 1 :(得分:0)
这只是解决方案之一:
首先添加每个复选框css类,如:“checkbox-option”
删除按钮添加css类'delete-btn'
然后这个jQuery代码:
$('.delete-btn').click(function(){
$('.checkbox-option').each(function(){
if($(this).is(':checked'){
//then goes larger part of your code:
var myid = $(this).attr('id');
$.post('newsdel.php', {delete_id:myid}, function(data) {
if(data == 'true') {
$('#'+myid).fadeOut();
var rowCount = $('#count tr').length;
if(rowCount < 3) {
window.location = '?news=1';
}
} else {
alert('Could not delete!');
}
});
}
});
});
这是在使用您的部分代码时,但它会为每个代码发送ajax请求。在迭代时,其他的将是收集数组中的所有ID(然后序列化)或用逗号分隔(然后在服务器端脚本中拆分),然后发送一个ajax请求。