jQuery實現checkbox即點即改大量刪除及中間遇到的坑,jquerycheckbox

來源:互聯網
上載者:User

jQuery實現checkbox即點即改大量刪除及中間遇到的坑,jquerycheckbox

 最近要用jQuery實現一個大量刪除操作,效果如

最終頁面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即點即改批量計數及中間遇到的坑,希望對大家有所協助,如果大家有任何疑問歡迎各我留言,小編會及時回複大家的!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.