温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jquery实现账单全部选中和部分选中管理

发布时间:2020-07-04 19:32:03 来源:网络 阅读:499 作者:jjjyyy66 栏目:web开发

在做购物车系统是我们往往会遇到这样一个需求,在点击全选框时我们要将全部的单个账单都选中;在单个选中账单时,如果账单全部被选中则需要全选框处于选中状态,若没有全部被选中则全选框处于没选中状态;

以下是在学习过程的实现代码:

<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='../p_w_picpath/computer.jpg' class='products' />"
+ "<a href='#'>联想笔记本电脑</a>"
+ "</td>"
+ "<td>¥3189元</td>"
+ "<td>"
+ "<img src='../p_w_picpath/subtraction.gif' width='20' height='20' />"
+ "<input type='text' class='quantity' value='1' />"
+ "<img src='../p_w_picpath/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实现账单全部选中和部分选中管理Jquery实现账单全部选中和部分选中管理

点击添加按钮可以添加预先设置好的元素及代码蓝色部分   效果如图

Jquery实现账单全部选中和部分选中管理

依次选中单个账单,当账单全部被选中时,全选按钮被设为选中状态,代码红色部分,若没全部选中时则状态不变  效果如图

Jquery实现账单全部选中和部分选中管理Jquery实现账单全部选中和部分选中管理


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI