时间:2021-07-01 10:21:17 帮助过:18人阅读
效果图如下所述:
—>—>—>
—>—>—>
创建视图show.php
- <?php
- header('content-type:text/html;charset=utf-8');
- $pdo=new PDO('mysql:host=localhost;dbname=***;','root','root');
- $pdo->exec('set names utf8');
- $sql='select * from ***';
- $info=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
- ?>
- <center>
- <table border="1">
- <tr>
- <td>id</td>
- <td>title</td>
- <td>content</td>
- </tr>
- <?php foreach($info as $k => $v){ ?>
- <tr>
- <td><input type="checkbox" name="box" value="<?= $v['id'] ?>"><?= $v['id'] ?></td>
- <td><?= $v['title'] ?></td>
- <td><?= $v['content'] ?></td>
- </tr>
- <?php } ?>
- </table>
- <button>批量删除</button>
- </center>
- <script src="../jquery.1.12.min.js"></script>
- <script>
- $(function(){
- $('button').click(function(){
- var ids=$(':checkbox');
- var str='';
- var count=0;
- for(var i=0;i<ids.length;i++){
- if(ids.eq(i).is(':checked')){
- str+=','+ids.eq(i).val();
- count++;
- }
- }
- var str=str.substr(1);
- if(confirm('你确定要删除这'+count+'条数据吗?')){
- //获取id后删除
- $.ajax({
- type:'get',
- url:'adminDel.php',
- data:{str:str},
- success:function(res){
- if(res>0){
- alert('删除成功');
- for(var i=ids.length-1;i>=0;i--){
- if(ids.eq(i).is(':checked')){
- ids.eq(i).parent().parent().remove();
- }
- }
- }
- }
- })
- }
- return false;
- /*var box=document.getElementsByName('box');
- var str="";
- for(var i=0;i<box.length;i++){
- if(box[i].checked==true){
- str+=','+box[i].value;
- }
- }
- var str=str.substr(1);
- alert(str);*/
- });
- })
- </script>
创建adminDel.php
- <?php
- header('content-type:text/html;charset=utf-8');
- $str=$_GET['str'];
- $pdo=new PDO('mysql:host=localhost;dbname=***;','root','root');
- $pdo->exec('set names utf8');
- $sql='delete from *** where id in ('.$str.')';
- $res=$pdo->exec($sql);
- //受影响行数
- echo $res;
- ?>
以上就是php操作jQuery如何实现批量删除?的详细内容,更多请关注Gxl网其它相关文章!