Js写购物车全选全不选计算单价总价代码

PHP交流群:294088839,

Python交流群:652376983



方法一:

<script>


function chk(){


var chkAll=document.getElementById('J_SelectAllCbx2');


var chks=document.getElementsByName('items');


for(var i=0;i<chks.length;i++){


chks[i].checked=chkAll.checked;


}


}


document.getElementById('J_SelectAllCbx2').οnclick=chk;






//减少数量


function decrement(n,id){


var buynum=document.getElementById('buynum'+n).value;


if(buynum>1){


num=document.getElementById('buynum'+n).value=parseInt(buynum)-1;


}


getprices();


gettotalprice();


//减少数量的时候对数据库中产品数量更新


$.post('/Home/MyCart/goodsNum.html',{num:num,id:id})


}


//增加数量


function increment(n,id){


var buynum=document.getElementById('buynum'+n).value;


if(buynum<199){


num=document.getElementById('buynum'+n).value=parseInt(buynum)+1;


}


getprices();


gettotalprice();


//增加数量的时候对数据库中产品数量更新


$.post('/Home/MyCart/goodsNum.html',{num:num,id:id})


}






function chgnum(v){


if(v.value<1){


v.value=1;


}


if(v.value>199){


v.value=199;


}


if(isNaN(v.value)){


v.value=1;


}






gettotalprice();


}










//获取每件商品的总价


function getprices(){


//每件商品的数量


var nums=document.getElementsByClassName('text_box');


//每件商品的单价


var price=document.getElementsByClassName('gd_price');


//每件商品的总价


var prices=document.getElementsByClassName('prc');


for(var i=0;i<price.length;i++){






prices[i].innerHTML=parseInt(price[i].innerHTML)*parseInt(nums[i].value);






};


}






//计算所有商品的总价


function gettotalprice(){


getprices();


var inputs=document.getElementsByName('items');


var prices=document.getElementsByClassName('prc');


var totalprice=0;


for(var i=0;i<inputs.length;i++){


if(inputs[i].checked){


totalprice+=parseInt(prices[i].innerHTML);


};


};


document.getElementById('totalprice').innerHTML=totalprice;


}


$(function(){


getprices();


gettotalprice();


});



 

方法二:

 

$(".add").each(function(i,e){
var cont = 1;
        var danjia=$(this).parent().parent().find('.pricecolor').text();


$(this).prev().change(function(){
cont = $(this).val();


$(this).parent().parent().find(".pricecolor").text(danjia*cont);


var priceAll = $("input:checkbox[class=checkbtn]:checked").parent().parent().find(".pricecolor");


var sum=0;
for(var j=0;j<priceAll.length;j++){
sum += priceAll[j].innerText-0;
};


$(".tot-price").text(sum);
});


//加
$(this).click(function(){
cont++;
$(this).prev().val(cont);


            $(this).parent().parent().find(".pricecolor").text(danjia*cont);


var priceAll = $("input:checkbox[class=checkbtn]:checked").parent().parent().find(".pricecolor");


var sum=0;
for(var j=0;j<priceAll.length;j++){
sum += priceAll[j].innerText-0;
};


$(".tot-price").text(sum);
});


//减
$(this).prev().prev().click(function(){
cont--;
if(cont<=1){
cont=1;
}
$(this).next().val(cont);


$(this).parent().parent().find(".pricecolor").text(danjia*cont);




var priceAll = $("input:checkbox[class=checkbtn]:checked").parent().parent().find(".pricecolor");


var sum=0;
for(var j=0;j<priceAll.length;j++){
sum += priceAll[j].innerText-0;
};


$(".tot-price").text(sum);
});
//取消input框
$(this).parent().parent().find(".checkbtn").click(function(){
var priceAll = $("input:checkbox[class=checkbtn]:checked").parent().parent().find(".pricecolor");


var sum=0;
for(var j=0;j<priceAll.length;j++){
sum += priceAll[j].innerText-0;
};


$(".tot-price").text(sum);
})
});

 


</script>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Json____

您的鼓励是我创作的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值