购物车(通用功能)

 
            
/*
模块:购物车(dom操作js - 计算js)
开发:yangsiyu
历史:2017-1-20
2017-02-04(
购物车计算范围修改
)
2017-02-11(
添加选中后背景颜色
)
2017-02-23(
根据库存控制数量添加最大值
2017-02-24(
无货,缺货,禁止点击并且图标灰色
)
*/
$(function(){
//初始化后判断==缺货,无货 禁止点击按钮
$(".less_stock .add-btn,.out_stock .add-btn").addClass("prohibit");
$(".less_stock .count-num,.out_stock .count-num").attr("readonly","readonly");
// 数量减
$(".stock .reduce-btn").click(function(e){
var _this = $(this),
count_num = _this.parent().find('.count-num'), //获取当前数量
sing_price = _this.closest("ul").find(".li-price span").text(), //获取单价
min = count_num.val() - 1, //数量减一
needNum = _this.closest(".li-count").find(".need_message_number").text(); //获取页面库存数量
needNum = parseInt(needNum);
//点击过后 数量小于库存数量 加号按钮恢复可点击状态
if(count_num.val() < needNum + 1 ){ //小于库存
console.log(needNum)
_this.closest(".li-count").find(".add-btn").addClass("unprohibit").removeClass("prohibit");
}
if(min <=1) min = 1; //数量减一 最小值为一
//单件商品总价(计算)
var result = "";
result = (min) * sing_price; //数量 * 单价
result = result.toFixed(2);
_this.closest("ul").find(".li-sum span").text(result); //单件商品总价(赋值)
count_num.val(parseInt(count_num.val()) - 1);
if (count_num.val() <= 1) {
count_num.val(1);
}else{
timer(0.5);
}
TotalPrice();
});

// 数量加
$(".stock .add-btn").click(function(e){
var _this = $(this),
count_num = _this.parent().find('.count-num'), //获取当前数量
sing_price = _this.closest("ul").find(".li-price span").text(), //获取单价
needNum = _this.closest(".li-count").find(".need_message_number").text(), //判断库存数量
max = parseInt(count_num.val()) + 1; //数量加一
needNum = parseInt(needNum);
if(parseInt(count_num.val()) == 200) return false ;
count_num.val(parseInt(count_num.val()) + 1);
if(count_num.val() > 199){ //大于最大值
count_num.val(200);
}else if(count_num.val() >= needNum ){ //大于库存
//重置数量
_this.closest(".li-count").find(".add-btn").addClass("prohibit").removeClass("unprohibit");
_this.closest(".li-count").find(".count-num").val(needNum);
// _this.addClass("prohibit").removeClass("unprohibit");
}
max = parseInt(count_num.val());
var result = "";
result = (max) * sing_price;
result = result.toFixed(2);
_this.closest("ul").find(".li-sum span").text(result); //单件商品总价(赋值)
TotalPrice();
timer(0.5);
});

// 倒计时控制数量传值
var countdown;
function timer(time){
clearInterval(countdown);
var i = time;
countdown = setInterval(function(){
if( i <= 1){
clearInterval(countdown);
}
i--;
},100);
}

//手动加
$(".stock .count-num").keyup(function(){
var count_num = $(this).parent().find('.count-num');
if(count_num.val() >= 200){
count_num.val(200);
}
});
$(".stock .count-num").blur(function(e){
var _this = $(this);
count_num = _this.parent().find('.count-num'), //当前数量
sing_price = _this.closest("ul").find(".li-price span").text(), //单价
needNum = _this.closest(".li-count").find(".need_message_number").text(); // 库存
needNum = parseInt(needNum);
var min = count_num.val();
if(min <=1){ //小于最小值
min = 1 ;
}else if(min > needNum ){ //大于库存
min = needNum;
_this.closest(".li-count").find(".add-btn").addClass("prohibit").removeClass("unprohibit");
}else if(min < needNum){ //小于库存
_this.closest(".li-count").find(".add-btn").addClass("unprohibit").removeClass("prohibit");
}
_this.closest(".li-count").find(".count-num").val(min); // 当前数量重新赋值
var result = "";
result = (min) * sing_price;
result = result.toFixed(2);
_this.closest("ul").find(".li-sum span").text(result); //重新给商品总价赋值
TotalPrice();
timer(0);
});

//计算价格
function TotalPrice() {
var allprice = 0; //总价
var allnum = 0; //商品件数
$(".one-shop").each(function() { //循环每个店铺
var oprice = 0; //店铺总价
$(this).find("input[name='ifcheckbox']").each(function() { //循环店铺里面的商品
if ($(this).is(":checked")) { //如果该商品被选中
allnum ++;
var num = parseInt($(this).closest(".info-item").find(".count-num").val()); //得到商品的数量
var price = parseFloat($(this).parents(".info-item").find(".li-price span").text()); //得到商品的单价
var total = price * num; //计算单个商品的总价
oprice += total; //计算该店铺的总价
}
});
var oneprice = parseFloat(oprice.toFixed(2)); //得到每个店铺的总价
allprice += oneprice; //计算所有店铺的总价
});
$(".AllTotal").text(allprice.toFixed(2)); //输出全部总价
$(".pronum").text(allnum); //选中件数
}

//删除
$(".delpro").bind("click",function(){
//弹出框
layer.open({
type: 1,
title: '提示',
area: ['auto', 'auto'],
content: $(".js_modal_second"),
btn:['确定','取消'],
btn1:function(){
//删除选择中所有商品
$("input[name='ifcheckbox']").each(function(){
if ($(this).is(":checked")) {

$(this).closest(".info-item").remove();
}
});
TotalPrice();
},
cancel: function(){
// alert(123);
//右上角关闭回调
}
});
});

$(".delthis").on("click",function(){
//删除当前选择项需要选中状态
//数据库中删除
var dataID = $(this).closest(".info-item").attr("data-id");
//删除当前选择项无选中状态(页面)
$(this).closest(".info-item").remove();
$()
TotalPrice();
});

//去结算弹出
$("#js_modal").on("click", function(){
//去结算获取选中商品的id&数量&单价
// $("input[name='ifcheckbox']").each(function(){
// if ($(this).is(":checked")){
// }
// });
//判断是否选中商品
var checkLen = $(".cart-info").find("input[type='checkbox']:checked").length;
var decision = true;
if(checkLen <= 0){
//弹出框
layer.open({
type: 1,
title: '提示',
area: ['auto', 'auto'],
content: $(".js_modal"),
cancel: function(){
// alert(123);
//右上角关闭回调
}
});
$("body").css("overflow-y","hidden");
}
});
//选中商品背景颜色
$("input[name='ifcheckbox']").on("click",function (){
TotalPrice();
$("input[name='ifcheckbox']").each(function() { //循环店铺里面的商品
if ($(this).is(":checked")){ //如果该商品被选中
$(this).closest(".info-item").addClass("info-item-check");
}else{
$(this).closest(".info-item").removeClass("info-item-check");
}
});
checkAll();
});

// 点击全选按钮
$(".checkbox-all").click(function() {
if ($(this).prop("checked") == true) { //如果全选按钮被选中
$("input").prop("checked",$(this).prop("checked"));
$(".info-item").addClass("info-item-check");
TotalPrice();
} else {
$(".info-item").removeClass("info-item-check");
$("input").prop("checked",$(this).prop("checked"));
TotalPrice();
}
});

//全选后点击选中项取消选中后全选框取消选中状态
function checkAll(){
var len = $("input[name='ifcheckbox']").length;
var count = 0;
$("input[name='ifcheckbox']").each(function(i,v){
if ($(this).is(":checked")){
count = count + 1;
if(len == count){
$(".checkbox-all").prop("checked",true);
}else{
$(".checkbox-all").prop("checked",false);
}
}
});
}
});
夜黑风高-ysy/snippet_file_0.txt
搜索框历史记录2.0
  最后更新时间 2017-03-23 13:13:13
  snippet_file_0.txt  158行  Text
Raw

猜你喜欢

转载自blog.csdn.net/m0_37400682/article/details/73467715