?php
require_once './db_conn.php';
$sql = "select * from user";
$result = mysqli_query($conn, $sql);
?>
html lang="zh-CN">
head>
meta charset="UTF-8">
title>全選演示/title>
meta http-equiv="X-UA-Compatible" content="IE=Edge">
link rel="stylesheet" type="text/css" href="./static/bootstrap.min.css" rel="external nofollow" >
script src="./static/jquery.js">/script>
meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0">
/head>
body>
form enctype="multipart/form-data" method="post">
div class="bs-example" data-example-id="simple-table" style="padding-left: 30px;">
table class="table" id="J-dl">
a href="javascript:void(0);" rel="external nofollow" class="btn btn-danger" onclick="selectAll()" title="刪除選定數(shù)據(jù)" style="font-weight:normal">批量刪除/a>
thead>
tr>
th>input type="checkbox" id="J-all" class="ckb">/th>
th>First Name/th>
th>Last Name/th>
th>Username/th>
/tr>
/thead>
tbody>
?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo 'tr>
th>input type="checkbox" class="ck" id="ck-1" value="'.$row['id'].'">/th>
th scope="row">'.$row['id'].'/th>
td>'.$row['username'].'/td>
td>'.$row['sort'].'/td>
/tr>';
}
?>
/tbody>
/table>
/div>
/form>
script>
(function () {
var $all = $('#J-all');
var $dl = $('#J-dl');
// 綁定全選按鈕點(diǎn)擊事件,讓下面所有的復(fù)選框是跟全選的一樣
$all.on('click', function () {
$dl.find('.ck').prop('checked', !!this.checked);
});
// 綁定點(diǎn)擊所有的復(fù)選框,點(diǎn)擊的時(shí)候判斷是否頁(yè)面中全選了
$dl.find('.ck').on('click', function () {
// 我只是喜歡用filter(fn),用選擇器也行
// 查找沒(méi)有選擇的元素
var $unSelectedElem = $dl.find('.ck').filter(function () {
return !this.checked;
});
// 如果有沒(méi)有選中的,則讓全選的取消
if ($unSelectedElem.length) {
$all.prop('checked', false);
}
else {
$all.prop('checked', true);
}
});
})();
/script>
script type="text/javascript">
function selectAll() {
var ids = '';
$(".ck").each(function() {
if ($(this).is(':checked')) {
ids += ',' + $(this).val(); //逐個(gè)獲取id值,并用逗號(hào)分割開(kāi)
}
});
ids = ids.substring(1); // 進(jìn)行id處理,去除第一位的逗號(hào)
if (ids.length == 0) {
alert('請(qǐng)至少選擇一項(xiàng)');
} else {
if (confirm("確定刪除選中的?")) {
$.ajax({
type: "post",
url: "piliangdo.php",
data: {
ids:ids
},
success: function(data) {
if(data.trim()=="yes")
{
alert("刪除成功");
location.reload() //刷新頁(yè)面
}
else
{
alert("刪除失敗");
}
}
});
}
}
}
/script>
/body>
/html>
?php
header("content-type:text/html;charset='utf-8'");
require_once './db_conn.php';
$ids = trim($_POST['ids']);
$ids = explode(',', $ids);
foreach ($ids as $key => $val) {
$del_sql = "DELETE FROM `user` WHERE id = '$val'";
$result = mysqli_query($conn, $del_sql);
}
if ($result) {
echo "yes";
}
else{
echo "no";
}
?>
以上所述是小編給大家介紹的PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!