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í)候判斷是否頁面中全選了 $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(); //逐個(gè)獲取id值,并用逗號(hào)分割開 } }); ids = ids.substring(1); // 進(jìn)行id處理,去除第一位的逗號(hào) if (ids.length == 0) { alert('請至少選擇一項(xiàng)'); } 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 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- jquery ajax實(shí)現(xiàn)批量刪除具體思路及代碼
- PHP jQuery+Ajax結(jié)合寫批量刪除功能
- JQuery+ajax實(shí)現(xiàn)批量上傳圖片(自寫)
- jQuery通過ajax快速批量提交表單數(shù)據(jù)
- JQuery DataTable刪除行后的頁面更新利用Ajax解決
- jQuery之a(chǎn)jax刪除詳解
- jQuery ajax中使用confirm,確認(rèn)是否刪除的簡單實(shí)例
- 如何從jQuery的ajax請求中刪除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)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08ThinkPHP實(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ù)庫sql語句、MVC各個(gè)模塊功能與實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫關(guān)聯(lián)查詢排序功能代碼
本篇文章主要介紹了Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫關(guān)聯(lián)查詢排序功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02