Jquery實現賬單全部選中和部分選中管理

來源:互聯網
上載者:User

標籤:alt   代碼   ext   product   move   input   元素   each   png   

在做購物車系統是我們往往會遇到這樣一個需求,在點擊全選框時我們要將全部的單個賬單都選中;在單個選中賬單時,如果賬單全部被選中則需要全選框處於選中狀態,若沒有全部被選中則全選框處於沒選中狀態;

以下是在學習過程的實現代碼:

<script type="text/javascript">
$(document).ready(function(){
//刪除當前行商品元素
//$(".del").click(function () {
//$(this).parent().parent().remove();
//});
/* $(".del").on("click",function () {
$(this).parent().parent().remove();
}); */

$(".del").live("click",function () {
$(this).parent().parent().remove();
});

$(".add").click(function () {
//建立新節點
var $newPro = $("<tr>"
+ "<td>"
+ "<input name=‘‘ type=‘checkbox‘ value=‘‘ />"
+ "</td>"
+ "<td>"
+ "<img src=‘../image/computer.jpg‘ class=‘products‘ />"
+ "<a href=‘#‘>聯想膝上型電腦</a>"
+ "</td>"
+ "<td>¥3189元</td>"
+ "<td>"
+ "<img src=‘../image/subtraction.gif‘ width=‘20‘ height=‘20‘ />"
+ "<input type=‘text‘ class=‘quantity‘ value=‘1‘ />"
+ "<img src=‘../image/add.gif‘ width=‘20‘ height=‘20‘ />"
+ "</td>"
+ "<td><a href=‘#‘ class=‘del‘>刪除</a></td>"
+ "</tr>");
//在table中插入建立節點
$("table").append($newPro);
});
$("button").bind({
click:function(){},
mouseover:function(){ },
mouseout:function(){ }
});

$("th input[type=‘checkbox‘]").on("click",function(){
  if($(this).attr("checked")=="checked"){//點擊全選複選框 全選所有商品
    var $selectAll = $("tr td input[type=‘checkbox‘]");
    //alert($selectAll.length);
    $selectAll.each(function(){
    $(this).attr("checked","checked");
  });
}else{//點擊全選複選框 取消全選所有商品
  var $selectAll = $("tr td input[type=‘checkbox‘]");
  //alert($selectAll.length);
 $selectAll.each(function(){
  $(this).attr("checked",false);
 });
}
});
 $("tr td input[type=‘checkbox‘]").live("click",function (){//給所有的checkbox多選框綁定click事件
  var i =0;//聲明一個變數記錄選中的個數
  $("tr td input[type=‘checkbox‘]").each(function(){
    if($(this).attr("checked")=="checked"){//如果選中記錄數+1
      i=i+1;
    };
  });
  if(i==$("tr td input[type=‘checkbox‘]").length){//如果全部選中則將全選按鈕設為選中狀態
    $("th input[type=‘checkbox‘]").attr("checked","checked");
  }else{
    $("th input[type=‘checkbox‘]").attr("checked",false);
  };
 });
});
</script>

實現效果:

點擊全選----則商品內容全部選中      取消選中全選則全部取消    代碼天藍色部分效果   

點擊添加按鈕可以添加預先設定好的元素及代碼藍色部分   效果

依次選中單個賬單,當賬單全部被選中時,全選按鈕被設為選中狀態,代碼紅色部分,若沒全部選中時則狀態不變  效果

 

**********************以上內容僅是學習總結,僅供參考**********************

Jquery實現賬單全部選中和部分選中管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.