jQuery实现checkbox即点即改批量删除及中间遇到的坑


Posted in jQuery onNovember 11, 2017

 最近要用jQuery实现一个批量删除操作,效果如下图

jQuery实现checkbox即点即改批量删除及中间遇到的坑

最终页面page.html,此页面使用了bootstrap和jQuery,如果没有需要下载一下

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>视频管理</title>
  <link rel="stylesheet" type="text/css" href="css/init.css" rel="external nofollow" />
  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" rel="external nofollow" />
  <script src="js/jquery-3.2.1.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <style type="text/css">
   .head {
    background-color: #EFEFF4;
    font-size: 16px;
    color: black;
    padding: 20px;
    font-size: 20px;
    overflow: hidden;
    margin-bottom: 10px;
   }
   .table {
    margin-top: 30px;
    border: 1px solid #CCCCCC;
   }
   .name_info {
    color: red;
   }
   .message p {
    margin: 0px;
    padding: 0px;
   }
   .form_0 {
    background-color: #EEEEEE;
   }
  </style>
 </head>
 <body>
  <div class="container-fluid">
   <div id="row" class="head">
    <div class="col-sm-12 col-md-12 col-lg-12">
     <p>视频管理</p>
    </div>
   </div>
   <div id="row" class="col-sm-12 col-md-12">
    <div class="col-xs-1">
     <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="batchDel" class="btn btn-danger btn">批量删除 <span class="badge" id="badge_0">0</span></a>
    </div>
    <div class="col-xs-1">
     <p class="text-danger">删除成功</p>
    </div>
    <div class="col-xs-1">
     <a href="speaker_add.html" rel="external nofollow" class="btn btn-primary btn">添加</a>
    </div>
    <div id="row" class="col-xs-6 col-md-offset-1">
     <form class="form-inline form_0" role="form">
      <div class="form-group col-xs-3 col-md-3">
       <input type="text" class="form-control" id="name" placeholder="课程名称">
      </div>
      <div class="form-group col-xs-2 col-md-2">
       <select class="form-control btn-primary">
        <option>请选择课程</option>
        <option>2</option>
       </select>
      </div>
      <div class="form-group col-xs-2 col-md-2">
       <select class="form-control btn-primary">
        <option>请选择老师</option>
        <option>2</option>
       </select>
      </div>
      <div class="form-group col-xs-2 col-md-2">
       <button type="submit" class="btn btn-primary">查询</button>
      </div>
     </form>
    </div>
   </div>
   <div class="row">
    <div class="col-md-10 col-lg-10">
     <table class="table table-striped">
      <thead>
       <tr>
        <th>选择</th>
        <th>序号</th>
        <th>名称</th>
        <th>介绍</th>
        <th>讲师</th>
        <th>时长</th>
        <th>播放次数</th>
        <th>编辑</th>
        <th>删除</th>
       </tr>
      </thead>
      <tbody>
       <tr>
        <td>
         <input class="check_0" value="0" type="checkbox">
        </td>
        <td class="show_id">1</td>
        <td id="show_name">环境搭建</td>
        <td>这视频真tm牛逼</td>
        <td>老王</td>
        <td>666</td>
        <td>666</td>
        <td>
         <a href="video_update.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="edit_submit"><span class="glyphicon glyphicon-pencil"></span>编辑</a>
        </td>
        <td>
         <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="delete" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-trash"></span>删除</a>
        </td>
       </tr>
       <tr>
        <td>
         <input class="check_0" value="1" type="checkbox">
        </td>
        <td class="show_id">1</td>
        <td id="show_name">环境搭建</td>
        <td>这视频真tm牛逼</td>
        <td>老王</td>
        <td>666</td>
        <td>666</td>
        <td>
         <a href="video_update.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="edit_submit"><span class="glyphicon glyphicon-pencil"></span>编辑</a>
        </td>
        <td>
         <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="delete" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-trash"></span>删除</a>
        </td>
       </tr>
       <tr>
        <td>
         <input class="check_0" value="2" type="checkbox">
        </td>
        <td class="show_id">1</td>
        <td id="show_name">环境搭建</td>
        <td>这视频真tm牛逼</td>
        <td>老王</td>
        <td>666</td>
        <td>666</td>
        <td>
         <a href="video_update.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="edit_submit"><span class="glyphicon glyphicon-pencil"></span>编辑</a>
        </td>
        <td>
         <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="delete" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-trash"></span>删除</a>
        </td>
       </tr>
      </tbody>
     </table>
    </div>
   </div>
   <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
     <div class="modal-content">
      <div class="modal-header">
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
       <h4 class="modal-title" id="myModalLabel">温馨提示</h4>
      </div>
      <div id="info" class="modal-body">你确定要删除吗?</div>
      <div class="modal-footer">
       <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
       <button type="button" id="delete_submit" class="btn btn-primary">确定</button>
      </div>
     </div>
     <!-- /.modal-content -->
    </div>
    <!-- /.modal -->
   </div>
  </div>
  <form id="form" method="post">
   <input type="hidden" name="_method" id="_method" value="DELETE" />
   <input type="hidden" name="id" id="id" />
  </form>
 </body>
 <script type="text/javascript">
  $(document).ready(function() {
   $("#delete").click(function() {
    var click_name = $(this).parent().siblings("#show_name").html();
    var click_id = $(this).parent().siblings("#show_id").html();
    $("#info").html("你确定要删除视频:<a class='name_info'>" + click_name + "</a>吗?");
    $("#id").val(click_id);
   })
   $("#delete_submit").click(function() {
    $("#_method").val("DELETE");
    $("#form").submit();
   })
   $("#edit_submit").click(function() {
    var click_id = $(this).parent().siblings("#show_id").html();
    $("#id").val(click_id);
    $("#form").attr("method", "get");
    $("#_method").val("get");
    $("#form").submit();
   })
   //获取选中的个数
   $(".check_0").click(function() {
    $("#badge_0").html($("input[type=checkbox]:checked").length);
    //alert($("input[type = checkbox]: checked ").length); 
   })
   //批量删除
   $("#batchDel").click(function() {
     var params = "";
     $("input[type = checkbox]:checked").each(function(index,element){
      //第一个id不需要加前缀
      if(index == 0) {
       params += "id=" +
        $(this).val();
      } else {
       params += "&id=" +
        $(this).val();
      }
     });
     alert("生成的拼接参数:" + params);
   })
  })
 </script>
</html>

上图的操作主要需要两个操作:

一、当checkBox点击时进行选中计数(带坑)。

二、当点击批量删除时,拼接选中单位的id,传递给个后台。

初看一下,感觉这些很好实现,并且实现的方法很多。当初我也是这么认为的,结果搞了一个上午.......,接下来就看看jQuery的一些坑。

我开始想法是每次点击checkBox时,对所有的checkBox进行each()遍历,如果是勾选状态就将Num++,最后将num的值赋给批量删除的数字小标签,顺便把id也拼接了。

$(".check_0").click(function(){
     var num=0;
    var del_str="";
    $(".check_0").each(function(){
     alert($(this).val()+":"+$(this).attr("checked"));
     if($(this).attr("checked")=="checked"){
      num++;
      del_str+=$(this).parent().siblings("show_id").html()+"/";
     }
     alert(this.checked);
    })
   })

当这段代码触发时,却发现一个诡异的现象:这段代码会在勾选效果前先触发。类似于beforeClick(),所以当代码遍历时,无法获取当前的勾选状态。

就这个问题,我在网上找了半天答案,硬是不知道如何解决,什么mouseup()都用了一下,都是beforeClick的效果。

最后我使用了另一种写法。

//获取选中的个数
   $(".check_0").click(function() {
    $("#badge_0").html($("input[type=checkbox]:checked").length);
    //alert($("input[type = checkbox]: checked ").length); 
   })
   //批量删除
   $("#batchDel").click(function() {
     var params = "";
     $("input[type = checkbox]:checked").each(function(index,element){
      //第一个id不需要加前缀
      if(index == 0) {
       params += "id=" +
        $(this).val();
      } else {
       params += "&id=" +
        $(this).val();
      }
     });
     alert("生成的拼接参数:" + params);
   })

成功运行出效果!

结论:

在click()中使用input[type=[checkbox]:checked]选择器获得勾选元素时,得到的就是勾选后的结果afterclick()。

而使用.each()遍历时,获取的是勾选前的效果beforeclick。

为什么一个click函数里,能得出beforeclick和aferclick两种效果?

坑哭......

总结

以上所述是小编给大家介绍的jQuery实现checkbox即点即改批量计数及中间遇到的坑,希望对大家有所帮助,如果大家有任何疑问欢迎各我留言,小编会及时回复大家的!

jQuery 相关文章推荐
jQuery 实现双击编辑表格功能
Jun 19 jQuery
浅谈事件冒泡、事件委托、jQuery元素节点操作、滚轮事件与函数节流
Jul 22 jQuery
jQuery读取本地的json文件(实例讲解)
Oct 31 jQuery
jQuery基于Ajax实现读取XML数据功能示例
May 31 jQuery
jQuery实现导航样式布局操作示例【可自定义样式布局】
Jul 24 jQuery
解决jQuery使用append添加的元素事件无效的问题
Aug 30 jQuery
jQuery选择器之基本过滤选择器用法实例分析
Feb 19 jQuery
jQuery 常用特效实例小结【显示与隐藏、淡入淡出、滑动、动画等】
May 19 jQuery
jQuery 选择方法及$(this)用法实例分析
May 19 jQuery
基于JavaScript或jQuery实现网站夜间/高亮模式
May 30 jQuery
jQuery加PHP实现图片上传并提交的示例代码
Jul 16 jQuery
jQuery带控制按钮轮播图插件
Jul 31 jQuery
浅谈jquery中ajax跨域提交的时候会有2次请求的问题
Nov 10 #jQuery
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
Nov 09 #jQuery
jQuery.Sumoselect插件实现下拉复选框效果
Nov 09 #jQuery
jQuery实现动态添加节点与遍历节点功能示例
Nov 09 #jQuery
简单实现jquery隔行变色
Nov 09 #jQuery
jQuery实现的简单无刷新评论功能示例
Nov 08 #jQuery
详解在vue-cli中引用jQuery、bootstrap以及使用sass、less编写css
Nov 08 #jQuery
You might like
PHP中的正规表达式(一)
2006/10/09 PHP
PHP输出日历表代码实例
2015/03/27 PHP
PHP实现多关键字加亮功能
2016/10/21 PHP
php 截取utf-8格式的字符串实例代码
2016/10/30 PHP
PHP定义字符串的四种方式详解
2018/02/06 PHP
PHP asXML()函数讲解
2019/02/03 PHP
tp5框架内使用tp3.2分页的方法分析
2019/05/05 PHP
基于逻辑运算的简单权限系统(实现) JS 版
2007/03/24 Javascript
基于jQuery的为attr添加id title等效果的实现代码
2011/04/20 Javascript
jquery 面包屑导航 具体实现
2013/06/05 Javascript
jquery等宽输出文字插件使用介绍
2013/09/18 Javascript
css结合js制作下拉菜单示例代码
2014/02/27 Javascript
jquery中append()与appendto()用法分析
2014/11/14 Javascript
JavaScript使用指针操作实现约瑟夫问题实例
2015/04/07 Javascript
深入浅析knockout源码分析之订阅
2016/07/12 Javascript
jQuery Ajax 加载数据时异步显示加载动画
2016/08/01 Javascript
jQuery实现立体式数字滚动条增加效果
2016/12/21 Javascript
Vue引入sass并配置全局变量的方法
2018/06/27 Javascript
JavaScript怎样在删除前添加确认弹出框?
2019/05/27 Javascript
js实现AI五子棋人机大战
2020/05/28 Javascript
微信小程序按顺序同步执行的两种方式
2019/12/20 Javascript
vue v-on:click传递动态参数的步骤
2020/09/11 Javascript
Python FTP两个文件夹间的同步实例代码
2018/05/25 Python
OpenCV2从摄像头获取帧并写入视频文件的方法
2018/08/03 Python
深入理解Django自定义信号(signals)
2018/10/15 Python
Python Django2.0集成Celery4.1教程
2019/11/19 Python
python的数学算法函数及公式用法
2020/11/18 Python
CSS3教程(7):CSS3嵌入字体
2009/04/02 HTML / CSS
html5图片上传预览示例分享
2014/04/14 HTML / CSS
Linux中如何用命令创建目录
2015/01/12 面试题
18岁生日感言
2014/01/12 职场文书
搬迁通知
2015/04/20 职场文书
springboot拦截器无法注入redisTemplate的解决方法
2021/06/27 Java/Android
详解Oracle块修改跟踪功能
2021/11/07 Oracle
Win11筛选键导致键盘失灵怎么解决? Win11关闭筛选键的技巧
2022/04/08 数码科技
Elasticsearch Recovery 详细介绍
2022/04/19 Java/Android