目錄結(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'); // 綁定全選按鈕點擊事件,讓下面所有的復(fù)選框是跟全選的一樣 $all.on('click', function () { $dl.find('.ck').prop('checked', !!this.checked); }); // 綁定點擊所有的復(fù)選框,點擊的時候判斷是否頁面中全選了 $dl.find('.ck').on('click', function () { // 我只是喜歡用filter(fn),用選擇器也行 // 查找沒有選擇的元素 var $unSelectedElem = $dl.find('.ck').filter(function () { return !this.checked; }); // 如果有沒有選中的,則讓全選的取消 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(); //逐個獲取id值,并用逗號分割開 } }); ids = ids.substring(1); // 進(jìn)行id處理,去除第一位的逗號 if (ids.length == 0) { alert('請至少選擇一項'); } else { if (confirm("確定刪除選中的?")) { $.ajax({ type: "post", url: "piliangdo.php", data: { ids:ids }, success: function(data) { if(data.trim()=="yes") { alert("刪除成功"); location.reload() //刷新頁面 } 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 實現(xiàn)批量刪除功能實例代碼小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
標(biāo)簽:安康 金華 溫州 紹興 呼倫貝爾 清遠(yuǎn) 萊蕪 綏化
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP ajax+jQuery 實現(xiàn)批量刪除功能實例代碼小結(jié)》,本文關(guān)鍵詞 PHP,ajax+jQuery,實現(xiàn),批量,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。