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实现图片平滑滚动详解
Mar 22 jQuery
JQuery 获取Dom元素的实例讲解
Jul 08 jQuery
jQuery获取复选框选中的当前行的某个字段的值
Sep 15 jQuery
jquery ajaxfileuplod 上传文件 essyui laoding 效果【防止重复上传文件】
May 26 jQuery
JQuery事件委托原理与用法实例分析
May 13 jQuery
JQuery样式操作、click事件以及索引值-选项卡应用示例
May 14 jQuery
jquery多级树形下拉菜单的实例代码
Jul 09 jQuery
jquery添加div实现消息聊天框
Feb 08 jQuery
jQuery 选择方法及$(this)用法实例分析
May 19 jQuery
jquery实现简易验证插件封装
Sep 13 jQuery
jQuery列表动态增加和删除的实现方法
Nov 05 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
【动漫杂谈】关于《请在T台上微笑》
2020/03/03 日漫
PHP类中Static方法效率测试代码
2010/10/17 PHP
PHP strncasecmp字符串比较的小技巧
2011/01/04 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
Linux下手动编译安装PHP扩展的例子分享
2014/07/15 PHP
PHP基于SMTP协议实现邮件发送实例代码
2017/04/27 PHP
Laravel程序架构设计思路之使用动作类
2018/06/07 PHP
javascript+dom树型菜单类,希望朋友们一起进步
2007/05/03 Javascript
jQuery创建自定义的选择器用以选择高度大于100的超链接实例
2015/03/18 Javascript
自定义百度分享的分享按钮
2015/03/18 Javascript
jQuery实现下拉框功能实例代码
2016/05/06 Javascript
详解使用nodeJs安装Vue-cli
2017/05/17 NodeJs
微信小程序 支付功能(前端)的实现
2017/05/24 Javascript
详解koa2学习中使用 async 、await、promise解决异步的问题
2018/11/13 Javascript
微信小程序自定义单项选择器样式
2019/07/25 Javascript
layer.open的自适应及居中及子页面标题的修改方法
2019/09/05 Javascript
Vue实现input宽度随文字长度自适应操作
2020/07/29 Javascript
Ruby使用eventmachine为HTTP服务器添加文件下载功能
2016/04/20 Python
Python下的Softmax回归函数的实现方法(推荐)
2017/01/26 Python
浅谈Tensorflow模型的保存与恢复加载
2018/04/26 Python
Python多继承顺序实例分析
2018/05/26 Python
Python中实现单例模式的n种方式和原理
2018/11/14 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
基于Python采集爬取微信公众号历史数据
2020/11/27 Python
HTML5页面无缝闪开的问题及解决方案
2020/06/11 HTML / CSS
英国综合网上购物商城:The Hut
2018/07/03 全球购物
电大毕业生自我鉴定
2013/11/10 职场文书
产品工艺师的岗位职责
2013/11/15 职场文书
大学生实习证明范本
2014/01/15 职场文书
建设单位项目负责人任命书
2014/06/06 职场文书
消防工作实施方案
2014/06/09 职场文书
家庭贫困证明范本(经典版)
2014/09/22 职场文书
2014年档案管理员工作总结
2014/12/01 职场文书
SQL之各种join小结详细讲解
2021/08/04 MySQL
使用CSS连接数据库的方式
2022/02/28 HTML / CSS
Python实现自动玩连连看的脚本分享
2022/04/04 Python