PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié)
目錄結(jié)構(gòu)

piliangshan.php
<?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>
piliangdo.php
<?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";
}
?>
總結(jié)
以上所述是小編給大家介紹的PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- jquery ajax實(shí)現(xiàn)批量刪除具體思路及代碼
- PHP jQuery+Ajax結(jié)合寫(xiě)批量刪除功能
- JQuery+ajax實(shí)現(xiàn)批量上傳圖片(自寫(xiě))
- jQuery通過(guò)ajax快速批量提交表單數(shù)據(jù)
- JQuery DataTable刪除行后的頁(yè)面更新利用Ajax解決
- jQuery之a(chǎn)jax刪除詳解
- jQuery ajax中使用confirm,確認(rèn)是否刪除的簡(jiǎn)單實(shí)例
- 如何從jQuery的ajax請(qǐng)求中刪除X-Requested-With
- jQuery+css3實(shí)現(xiàn)Ajax點(diǎn)擊后動(dòng)態(tài)刪除功能的方法
- 基于php(Thinkphp)+jquery 實(shí)現(xiàn)ajax多選反選不選刪除數(shù)據(jù)功能
- jQuery+ajax實(shí)現(xiàn)批量刪除功能完整示例
相關(guān)文章
詳解thinkphp+redis+隊(duì)列的實(shí)現(xiàn)代碼
本篇文章主要介紹了thinkphp+redis+隊(duì)列的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
ThinkPHP實(shí)現(xiàn)批量刪除數(shù)據(jù)的代碼實(shí)例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)批量刪除數(shù)據(jù)的代碼實(shí)例,需要的朋友可以參考下2014-07-07
基于Codeigniter框架實(shí)現(xiàn)的student信息系統(tǒng)站點(diǎn)動(dòng)態(tài)發(fā)布功能詳解
這篇文章主要介紹了基于Codeigniter框架實(shí)現(xiàn)的student信息系統(tǒng)站點(diǎn)動(dòng)態(tài)發(fā)布功能,詳細(xì)分析了動(dòng)態(tài)站點(diǎn)相關(guān)的數(shù)據(jù)庫(kù)sql語(yǔ)句、MVC各個(gè)模塊功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03
Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能代碼
本篇文章主要介紹了Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
PHP 實(shí)現(xiàn)數(shù)組分頁(yè)
在日常開(kāi)發(fā)的業(yè)務(wù)環(huán)境中,我們一般都會(huì)使用MySQL語(yǔ)句來(lái)實(shí)現(xiàn)分頁(yè)的功能。但是,往往也有些數(shù)據(jù)并不多,或者只是獲取 PHP 中定義的一些數(shù)組數(shù)據(jù)時(shí)需要分頁(yè)的功能。這時(shí),我們可以在一次查詢中把所有的數(shù)據(jù)取出來(lái),然后在 PHP 的代碼層面進(jìn)行分頁(yè)功能的實(shí)現(xiàn)2021-06-06
一個(gè)php導(dǎo)出oracle庫(kù)的php代碼
紅色字為變量,我也是邊查手冊(cè)邊寫(xiě)的,以前沒(méi)有接觸過(guò),這段小代碼用了一個(gè)多小時(shí),記錄一下2009-04-04

