您的位置:首页 > Web前端 > JavaScript

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

2017-12-19 12:07 417 查看
技术QQ交流群:294088839

方法一:

<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').onclick=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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐