亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2018年12月06日 16:39:55   作者:PHP,Python  
這篇文章主要介紹了PHP ajax+jQuery 實(shí)現(xiàn)批量刪除功能實(shí)例代碼小結(jié),代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(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)站的支持!

相關(guān)文章

最新評論