本文实例讲述了php+ajax简单实现全选删除的方法。分享给大家供大家参考,具体如下:
1
2
|
< input type = "checkbox" id = "ckb_selectAll" onclick = "selectAll()" title = "选中/取消选中" >
< a href = "javascript:void(0);" onclick = "del_()" title = "删除选定数据" style = "font-weight:normal" >删除</ a >
|
↑全选checkbox
1
|
< input type = "checkbox" class = "ckb" id = "+con.id+" value = "+con.id+" >
|
↑为删除项,同一命名class为ckb,方便操作,同时将id值巧妙的放入input中,方便获取。
1
2
3
4
5
6
7
|
function selectAll() {
if ($( '#ckb_selectAll' ).is( ':checked' )) {
$( ".ckb" ).attr( "checked" , true ); //全部选中
} else {
$( ".ckb" ).attr( "checked" , false ); //全部取消
}
}
|
↑选中事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
function del_() {
var ids = '' ;
$( ".ckb" ).each( function () {
if ($( this ).is( ':checked' )) {
ids += ',' + $( this ).val(); //逐个获取id
}
});
ids = ids.substring(1); // 对id进行处理,去除第一个逗号
if (ids.length == 0) {
alert( '请选择要删除的选项' );
} else {
if (confirm( "确定删除?删除后将无法恢复。" )) {
url = "action=del_call_record&ids=" + ids;
$.ajax({
type: "post" ,
url: "send.php" ,
data: url,
success: function (json) {
if (parseInt(json.counts) > 0) {
alert(json.des);
location.reload();
} else {
alert(json.des);
}
},
error: function (XMLHttpRequest, textStatus) {
alert( "页面请求错误,请检查重试或联系管理员!\n" + textStatus);
}
});
}
}
}
|
↑删除用ajax来处理。
↓后台操作数据库,处理删除动作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$ids = trim( $_REQUEST [ 'ids' ]);
$del_sql = "DELETE FROM vicidial_call_record WHERE id IN(" . $ids . ")" ;
//print_r($del_sql);exit;
if (mysqli_query( $db_conn , $del_sql )) {
$counts = "1" ;
$des = "成功" ;
} else {
$counts = "0" ;
$des = "失败" ;
}
$json_data = "{" ;
$json_data . = "\"counts\":" .json_encode( $counts ). "," ;
$json_data . = "\"des\":" .json_encode( $des ). "" ;
$json_data . = "}" ;
echo $json_data ;
break ;
|
完成
希望本文所述对大家PHP程序设计有所帮助。