//加入购物车 function add_shop_cart(id,callback){ $.ajax({ url : "/index.php?m=Shop&c=ShopCart&a=add_shop_cart", type : "post", data : {"id":id,num:$(".quantity").val()}, dataType : "json", success : function(data){ if(data.status == 0){ localStorage.setItem("backurl",window.location.href); window.location.href = "/index.php?m=Member&c=Public&a=login"; }else{ if(callback){ callback(); }else{ layer.msg(data.info); } } } }); } //加入购物车 function add_shop_cart2(id,obj){ $.ajax({ url : "/index.php?m=Shop&c=ShopCart&a=add_shop_cart", type : "post", data : {"id":id,num:$(obj).parent().find(".quantity").val()}, dataType : "json", success : function(data){ if(data.status == 0){ localStorage.setItem("backurl",window.location.href); window.location.href = "/index.php?m=Member&c=Public&a=login"; }else{ layer.msg(data.info); } } }); } //加入购物车并立即购买 function add_shop_cart_andBuy(id){ add_shop_cart(id,function(){ window.location.href="/index.php?m=Shop&c=ShopCart&a=index"; }); } // var addressType; //弹出收件地址窗口 function addressList(type){ if(type){ addressType=type; } $.get("/index.php?m=Shop&c=ShopCart&a=addressList",function(result){ $("body").append(result); $(".SelectAddress").fadeIn(); $(".bg2").show(); }); } //关闭收件地址窗口 function closeAddressList(){ $(".SelectAddress").remove(); $(".bg2").remove(); } //新增地址 function addAddress(){ $.get("/index.php?m=Shop&c=ShopCart&a=addAddress",function(result){ $("body").append(result); $(".NewAddress").fadeIn(); $("#distpicker5").distpicker({ province: "---- 所在省 ----", city: "---- 所在市 ----", district: "---- 所在区 ----" }); }); } //新增地址 function editorAddress(id){ $.get("/index.php?m=Shop&c=ShopCart&a=editorAddress&id="+id,function(result){ $("body").append(result); $(".NewAddress").fadeIn(); }); } //关闭新增地址窗口 function closeAddAddress(){ $(".NewAddress").fadeOut(); $(".NewAddress").remove(); } //确定收货地址 function setShAddress(){ var ShAddress=$("input[name='sh_address_id']:checked"); if(!ShAddress.val()){ layer.alert("请选择地址"); return ; }else{ var str =ShAddress.next().html(); if(addressType=="sh"){ $("input[name='order_sh_address_id']").val(ShAddress.val()); $("#sh_address_str").html(str); }else{ $("input[name='order_sp_address_id']").val(ShAddress.val()); $("#sp_address_str").html(str); } closeAddressList(); } } $(function () { //全局的checkbox选中和未选中的样式 var $allCheckbox = $('input[type="checkbox"]'), $wholeChexbox = $('.whole_check'), $cartBox = $('.cartBox'), $shopCheckbox = $('.shopChoice'), $sonCheckBox = $('.son_check'); $allCheckbox.click(function () { if ($(this).is(':checked')) { $(this).next('label').prop("checked", true); } else { $(this).next('label').prop("checked", false); } }); //===============================================全局全选================================ $wholeChexbox.click(function () { var $checkboxs = $cartBox.find('input[type="checkbox"]'); if ($(this).is(':checked')) { $checkboxs.prop("checked", true); } else { $checkboxs.prop("checked", false); } totalMoney(); }); $("#deleteAll").click(function(){ $(".order_lists").each(function(){ if ($(this).find('input[type="checkbox"]').is(':checked')) { var delete_id=$(this).data("id"); var obj=this; $.get(delete_url,{"delete_id":delete_id},function(data){ if(data.status == 1){ $(obj).remove(); $sonCheckBox = $('.son_check'); totalMoney(); } },'json') } }) }) $sonCheckBox.each(function () { $(this).click(function () { if ($(this).is(':checked')) { //判断:所有是否勾选 var len = $sonCheckBox.length; var num = 0; $sonCheckBox.each(function () { if ($(this).is(':checked')) { num++; } }); if (num == len) { $wholeChexbox.prop("checked", true); } } else { //单个取消勾选,全局全选取消勾选 $wholeChexbox.prop("checked", false); } }) }) //=======================================每个checkbox与全选checkbox的关系/每个与其下样式的变化=================================================== //有一个未选中,全局全选按钮取消对勾,若全选中,则全局全选按钮打对勾。 $shopCheckbox.each(function () { $(this).click(function () { if ($(this).is(':checked')) { //判断:全选中,则全局全选按钮打对勾。 var len = $shopCheckbox.length; var num = 0; $shopCheckbox.each(function () { if ($(this).is(':checked')) { num++; } }); if (num == len) { $wholeChexbox.prop("checked", true); } //下的checkbox选中状态 $(this).parents('.cartBox').find('.son_check').prop("checked", true); } else { //否则,全局全选按钮取消对勾 $wholeChexbox.prop("checked", false); $wholeChexbox.next('label').removeClass('mark'); //下的checkbox选中状态 $(this).parents('.cartBox').find('.son_check').prop("checked", false); } totalMoney(); }); }); //========================================每个checkbox与其下的checkbox的关系====================================================== //$sonChecks有一个未选中,全选按钮取消选中,若全都选中,则全选打对勾 $cartBox.each(function () { var $this = $(this); var $sonChecks = $this.find('.son_check'); $sonChecks.each(function () { $(this).click(function () { if ($(this).is(':checked')) { //判断:如果所有的$sonChecks都选中则全选打对勾! var len = $sonChecks.length; var num = 0; $sonChecks.each(function () { if ($(this).is(':checked')) { num++; } }); if (num == len) { $(this).parents('.cartBox').find('.shopChoice').prop("checked", true); } } else { //否则,全选取消 $(this).parents('.cartBox').find('.shopChoice').prop("checked", false); } totalMoney(); }); }); }); //=================================================数量============================================== var $plus = $('.plus'), $reduce = $('.reduce'), $all_sum = $('.sum'); $plus.click(function () { var $inputVal = $(this).prev('input'), $count = parseInt($inputVal.val())+1, $obj = $(this).parents('.amount_box').find('.reduce'), $priceTotalObj = $(this).parents('.order_lists').find('.sum_price'), $price = $(this).parents('.order_lists').find('.price').html(), //增加 $priceTotal = $count*parseInt($price.substring(1)); $inputVal.val($count); $priceTotalObj.html('¥'+$priceTotal.toFixed(2)); if($inputVal.val()>1 && $obj.hasClass('reSty')){ $obj.removeClass('reSty'); } var id = $(this).data("id"); change_shop_cart(id,$count); totalMoney(); }); $reduce.click(function () { var $inputVal = $(this).next('input'), $count = parseInt($inputVal.val())-1, $priceTotalObj = $(this).parents('.order_lists').find('.sum_price'), $price = $(this).parents('.order_lists').find('.price').html(), //减少 $priceTotal = $count*parseInt($price.substring(1)); if($count<1){ layer.alert("商品数量不能小于1件"); return; } if($inputVal.val()>1){ $inputVal.val($count); $priceTotalObj.html('¥'+$priceTotal.toFixed(2)); } if($inputVal.val()==1 && !$(this).hasClass('reSty')){ $(this).addClass('reSty'); } var id = $(this).data("id"); change_shop_cart(id,$count); totalMoney(); }); $all_sum.keyup(function () { var $count = 0, $priceTotalObj = $(this).parents('.order_lists').find('.sum_price'), $price = $(this).parents('.order_lists').find('.price').html(), //单价 $priceTotal = 0; if($(this).val()==''){ $(this).val('1'); } $(this).val($(this).val().replace(/\D|^0/g,'')); $count = $(this).val(); $priceTotal = $count*parseInt($price.substring(1)); $(this).attr('value',$count); $priceTotalObj.html('¥'+$priceTotal.toFixed(2)); var id = $(this).data("id"); change_shop_cart(id,$count); totalMoney(); }) //======================================移除======================================== var $order_lists = null; var $order_content = ''; var delete_id; $('.delBtn').click(function () { delete_id = $(this).data("id"); $order_lists = $(this).parents('.order_lists'); $order_content = $order_lists.parents('.order_content'); $('.model_bg').fadeIn(300); $('.my_model').fadeIn(300); }); //关闭模态框 $('.closeModel').click(function () { closeM(); }); $('.dialog-close').click(function () { closeM(); }); function closeM() { $('.model_bg').fadeOut(300); $('.my_model').fadeOut(300); } //确定按钮,移除 $('.dialog-sure').click(function () { $.get(delete_url,{"delete_id":delete_id},function(data){ if(data.status == 1){ closeM(); $order_lists.remove(); closeM(); $sonCheckBox = $('.son_check'); totalMoney(); } },'json') }) function change_shop_cart(id,num){ $.ajax({ "url" :change_shop_cart_url, "type" : "get", dataType : "json", data : {"id":id,num:num}, success : function(data){ console.log(data); } }); } //======================================总计========================================== function totalMoney() { var total_money = 0; var total_count = 0; var calBtn = $('.mygwc_jieg_right input'); $sonCheckBox.each(function () { if ($(this).is(':checked')) { var goods = parseInt($(this).parents('.order_lists').find('.sum_price').html().substring(1)); var num = parseInt($(this).parents('.order_lists').length); total_money += goods; total_count += num; } }); $('.total_text').html('¥'+total_money.toFixed(2)); $('.piece_num').html(total_count); // console.log(total_money,total_count); if(total_money!=0 && total_count!=0){ if(!calBtn.hasClass('on')){ calBtn.addClass('on');//增加样式 calBtn.removeAttr("disabled");//启用按钮 } }else{ if(calBtn.hasClass('on')){ calBtn.removeClass('on');//去除样式 calBtn.attr("disabled", "true");//关闭按钮 } } } });