當前位置:首頁 » 網購平台 » js增減購物車結算
擴展閱讀
寧波奧德賽優惠價格 2021-03-15 14:26:02
丹尼斯購物卡能掛失么 2021-03-15 14:25:58
淘寶購物指紋驗證失敗 2021-03-15 14:24:44

js增減購物車結算

發布時間: 2021-02-04 13:50:35

『壹』 以下代碼里,怎麼樣用JS增添一個刪除鍵,刪除購物車內對應的商品

<!doctypehtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding:0;}
#div_idp{width:80px;height:30px;text-align:left;border:1pxsolid#000;line-height:30px;}
#div_idpbutton{float:right;height:30px;}
</style>
<scriptsrc="js/tool.js"></script>
<script>
window.onload=function(){
vardiv=document.getElementById("div_id");
varbutton=document.getElementsByTagName("button")
for(vari=0;i<button.length-1;i++){
button[i].onclick=function(){
alert("加入購物車成功");
varp=document.createElement("p");
varpContent=document.createTextNode(this.innerHTML);
p.appendChild(pContent);
div.appendChild(p);
vardelBtn=document.createElement("button");
vardelBtnContent=document.createTextNode("x");
delBtn.appendChild(delBtnContent);
p.appendChild(delBtn);
delBtn.onclick=function(){
div.removeChild(p);
}
//setCookie("購物",div.innerHTML,getDate(100))
}
}

varisTrue=false;
varbutton1=document.getElementById("button_id")
button1.onclick=function(){
if(isTrue==false){
button1.innerHTML="隱藏購物車"
isTrue=true;
/*if(getCookie("購物")!=undefined){
div.innerHTML=getCookie("購物");
}*/
div.style.display="block";
}elseif(isTrue){
button1.innerHTML="顯示購物車"
isTrue=false;
div.style.display="none";
/*if(getCookie("購物")!=undefined){
div.innerHTML=getCookie("購物");
div.style.display="none";
}*/
}
}
}
</script>
</head>
<body>
<button>商品1</button>
<button>商品2</button>
<button>商品3</button>
<button>商品4</button>
<button>商品5</button><br/><br/>
<buttonid="button_id">顯示購物車</button>
<divid="div_id"style="display:none;"></div>

</body>
</html>

『貳』 購物車中 用滑鼠點擊文本框前的『+』號 或後面的『-』號時 數量相應增減 那段代碼是JS么 是的話怎麼寫的

var count=1;
if(按鈕被點擊操作){//代碼自己找

count=count+1;
}else if(減號按鈕被點擊操作){
count=count-1;
}

『叄』 如何用jquery寫多個購物車的數量曾減,我用jquery寫了但只第一個購物車增減有用,其它的無效,代碼如下:

這個時候就像前面大哥說的 不能用來表示了 可以用class
還有你的這個結構是不規范的 id一個頁面只能是唯一的
為了不同時改變 其他的購物車的數量 可以來找到相對的

這個結構:
<div>
<p>單價:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<inputclass="add" name="" type="button" value="+" />
<p>總價:<label class="total"></label></p>
</div>
<div>
<p>單價:3.95</p>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="4" />
<input class="add" name="" type="button" value="+" />
<p>總價:<label class="total"></label></p>
</div>

把每個購物車用一個div包起來

js:
$(function(){
//var t = $("#text_box");
$(".add").click(function(){
var t= $(this).parent().find(".text_box"); //根據這個來找到它相對的元素 當然也可以直接 //$(this).prev(); 這個直接來找到
t.val(parseint(t.val()+1);
setTotal($(this),t.val());
})
$(".min").click(function(){
var t= $(this).parent().find(".text_box"); //根據這個來找到它相對的元素 當然也可以直接 //$(this).next(); 這個直接來找到
t.val(parseint(t.val()-1);
setTotal($(this),t.val());
})
function setTotal(obj,number){
var total=number*$(obj).parent().find("p:first").text(); //這里算出總價
$(obj)).parent().find("total").html(total.toFixed(2));
}
// setTotal();
})

給一個建議 以後要想做好東西 布局一定要好

一個好的結構 才能讓你任意操作

『肆』 一些電商網站在加入購物車和結算的時候鏈接顯示JavaScript:void(0),為什麼要用js來進行提交

為了更好的用戶體驗和安全方面的考慮. 在加入購物車的時候, 一般是用ajax做的, 那麼如回果不用ajax做的話, 你點答擊加入購物車, 那麼就直接跳到購物車界面去了, 這對用戶體驗其實並不好, 因為用戶可能還想逛逛, 還想買東西.
結算的時候也是使用ajax, 為什麼, 因為結算頁面並不是死的頁面, 有可能用戶還要調一下商品數量啊, 再選一下款式啊什麼的, 這個時候你用ajax在本頁面進行數據的交換 , 就比不停的刷新頁面要好的多.

『伍』 用html做一個購物車,能實現簡單的產品數量和價格的加減就行。最後能計算出提交物品價格的總和。

html中的購物車的增減不能直接傳送到後台,可以通過ajax,在js中發送ajax

純前端的話可以參考下面的

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>cart</title>
<styletype="text/css">
body,p,a,input{
margin:0;
padding:0;
font-size:12px;
}

.container{
width:100%;
}

.main{
width:1000px;
height:500px;

margin:100pxauto;
}

.main.cart-containertable{
width:100%;}

.main.cart-containertabletr{
text-align:center;

}

.main.cart-containertabletr:hover{
background:rgba(128,128,128,0.2);
}
.main.cart-containertable.table-header{
height:30px;
background:#d9d9d9;

font-size:1.2em;

}

.main.cart-containertable.table-headertd:first-child{
border-left:solid4pxred;
box-sizing:border-box;
}

.main.cart-containertabletrtd:nth-child(1),
.main.cart-containertabletrtd:nth-child(2){
text-align:left;

}

.main.cart-containertabletrtd:nth-child(2){
width:52%;
}
.main.cart-containertabletrtd:nth-child(3){
width:12%;
}
.main.cart-containertabletrtd:nth-child(4){
width:12%;
}
.main.cart-containertabletrtd:nth-child(5){
width:12%;
}
.main.cart-containertabletrtd:last-child{
width:10%;
}

.cart-good{

height:60px;
}
.cart-goodimg{
float:left;
margin:10px;
width:60px;
}

.cart-goodtdp{
margin:10px0px;
}


/*加、減按鈕*/

.cart-goodtdinput[type='button']{
width:20px;
height:20px;
background:#00f300;
outline:none;
border:none;
}

.cart-goodtdinput[type='button']:disabled{
background:grey;
}

.cart-goodtdinput[type='button']:first-child{
margin-right:-4px;
}
.cart-goodtdinput[type='button']:last-child{
margin-left:-4px;
}
.cart-goodtdinput[type='text']{
width:30px;
height:20px;
outline:none;
border:none;
text-align:center;


}


.table-footer{
display:flex;
justify-content:space-between;
line-height:40px;
}


.table-footerdiv{
font-size:1.2em;

}
.table-footerdivbutton{
background:red;
width:120px;
height:40px;
color:white;
}
</style>
</head>
<body>
<divclass="container">
<header></header>
<sectionclass="main">
<divclass="cart-container">
<tablecellspacing="0">
<trclass="table-header">
<td><inputtype="checkbox"id="chk_alla"></td>
<td>聚美優品發貨</td>
<td>聚美價</td>
<td>數量</td>
<td>小計</td>
<td>操作</td>
</tr>
<!--<trclass="cart-good">-->
<!--<td><inputtype="checkbox"id="001"></td>-->
<!--<td>-->
<!--<imgsrc="https://p2.jmstatic.com/proct/001/293/1293263_std/1293263_60_60.jpg"alt="">-->
<!--<p>[極速免稅]PITTAMASK口罩3枚入</p>-->
<!--<p>型號:新版防曬款容量:3枚入</p>-->
<!--</td>-->
<!--<td>89.00</td>-->
<!--<td>-->
<!--<inputtype="button"value="-">-->
<!--<inputtype="text"value="1">-->
<!--<inputtype="button"value="+">-->
<!--</td>-->
<!--<td>89.00</td>-->
<!--<td><ahref="#">刪除</a></td>-->
<!--</tr>-->
</table>
<divclass="table-footer">
<div>
<inputtype="checkbox"id="chk_allb"><labelfor="chk_allb">全選</label>
<spanstyle="margin-left:20px">繼續購物|清空選中商品</span>
</div>
<div>
共<spanid="good_count">5</span>件商品商品應付總額:<spanid="goods_total">¥229.00</span>
<buttonclass="btn_menu">去結算</button>
</div>
</div>
</div>
</section>
</div>
<script>
(function(){
varskin_procts=[
{
"id":"002",
"title":"EsteeLauder多效智妍精華霜15ml",
"img_url":"http://p0.jmstatic.com/proct/003/565/3565880_std/3565880_350_350.jpg",
"price":249.0,
"number":6,
"acount":"520",
"ischecked":true
},
{
"id":"004",
"title":"EsteeLauder肌透修護潔面乳30ml",
"img_url":"http://p4.jmstatic.com/proct/003/155/3155764_std/3155764_350_350.jpg",
"price":49.9,
"number":1,
"acount":"5911",
"ischecked":false
},
{
"id":"008",
"title":"雅詩蘭黛無痕持妝粉底液",
"img_url":"http://p3.jmstatic.com/proct/003/662/3662318_std/3662318_350_350.jpg",
"price":69.9,
"number":2,
"acount":"3972",
"ischecked":true
},
{
"id":"0012",
"title":"EsteeLauder肌初賦活原生液30ml",
"img_url":"http://p4.jmstatic.com/proct/003/565/3565914_std/3565914_350_350.jpg",
"price":159.0,
"number":1,
"acount":"2338"
},
{
"id":"001",
"title":"雅詩蘭黛無痕持妝粉底液30ml",
"img_url":"http://p2.jmstatic.com/proct/001/648/1648502_std/1648502_350_350.jpg",
"price":298.0,
"number":4,
"acount":"5071",
"ischecked":false
},
{
"id":"009",
"title":"雅詩蘭黛眼部精華霜15ml",
"img_url":"http://p1.jmstatic.com/proct/001/049/1049746_std/1049746_350_350.jpg",
"price":399.0,
"number":1,
"acount":"4022",
"ischecked":false
}
]

//添加商品

functionload(){
vartbody=document.querySelector('.cart-containertabletbody');
for(letgoodofskin_procts){
tbody.innerHTML+=`<trclass="cart-good"id="${good.id}">
<td><inputtype="checkbox"class="good-check"${good.ischecked?"checked":''}></td>
<td>

<imgsrc="${good.img_url}"alt="">
<p>[極速免稅]PITTAMASK口罩3枚入</p>
<p>型號:新版防曬款容量:3枚入</p>

</td>
<td>${good.price}</td>
<td>

<inputtype="button"value="-"${good.number<=1?"disabled":''}>
<inputtype="text"value="${good.number}">
<inputtype="button"value="+">
</td>
<td>${good.price*good.number}</td>
<td><ahref="#">刪除</a></td>
</tr>`
}

totalAcount();
}
load();
//endall..........

//1.為table注冊單擊事件

vartable01=document.querySelector('.cart-containertable');

table01.onclick=function(event){

varnode=event.target
if(node.getAttribute('type')=='button'){
//alert(event.target.value);
changeNumber(event);
subtotal(event);
checkedRow(event);
checkedAllRows();

}elseif(node.className=='good-check'){
checkedAllRows();

}elseif(node.id=='chk_alla'){
varf=event.target.checked;
varchks=document.querySelectorAll('.good-check');
for(varckofchks){
ck.checked=f;
}

for(vargoodofskin_procts){
good.ischecked=f;
}


}elseif(node.nodeName.toLowerCase()=='a'){
vartr=event.target.parentNode.parentNode;
for(vari=0;i<skin_procts.length;i++){
if(skin_procts[i].id==tr.id){
skin_procts.splice(i,1);
console.log(skin_procts);
}
}

tr.parentNode.removeChild(tr);

}
totalAcount();
};

//單擊增加或減少按鈕的方法
functionchangeNumber(event){

varnode=event&&event.target;
varv=0;
if(node.value&&node.value=='+'){
//node.previousElementSibling.value=parseInt(node.previousElementSibling.value)+1;
node.previousElementSibling.value++;
v=node.previousElementSibling.value;
node.previousElementSibling.previousElementSibling.disabled=false;

}else{
//if(node.value&&node.value=='+')
if(node.nextElementSibling.value>1){
node.nextElementSibling.value--;
v=node.nextElementSibling.value;
if(v==1){
node.disabled=true;
}
}
}


//存儲商品數量

varid=node.parentNode.parentNode.id;

for(vargoodofskin_procts){
if(id==good.id){
good.number=v;
}
}

}

//每個商品小計的方法

functionsubtotal(event){
varnode=event&&event.target;

//varid=node.parentNode.parentNode.id;

//for(vargofskin_procts){
//if(g.id==id){
//alert(g.price);
//}
//}


//varprice=;

varprice=node.parentNode.previousElementSibling.innerText;
varnum=node.parentNode.children[1].value;
node.parentNode.nextElementSibling.innerText=(num*price).toFixed(2);
}

//檢驗該商品是否選中
functioncheckedRow(event){
event.target.parentNode.parentNode.firstElementChild.firstElementChild.checked=true;
//event.target.parentNode.parentNode.cells[0].firstElementChild.checked=true;
//vartbody=event.target.parentNode.parentNode.parentNode;
//event.target.parentNode.parentNode.parentNode.rows[3].cells[0].firstElementChild.checked=true;
}
//檢查是否全選

functioncheckedAllRows(){
varchks=document.querySelectorAll('.good-check');
varflag=true;
for(varckofchks){
if(!ck.checked){
flag=false;
break;
}
}
document.querySelector('#chk_alla').checked=flag;

}


//統計商品總量和總價格

functiontotalAcount(){

vartotal=0;
vartotal_price=0;
varchks=document.querySelectorAll('.good-check');
for(varckofchks){
if(ck.checked){
id=ck.parentNode.parentNode.id;
for(vargoodofskin_procts){
if(id==good.id){
total+=~~good.number;
total_price=total_price+(good.number*good.price);
good.ischecked=true;


}
}


}
}

document.querySelector('#good_count').innerText=total;
document.querySelector('#goods_total').innerText=total_price;


}

})();
</script>
</body>
</html>

『陸』 在java中,怎麼通過javascript來實現購物車里所有商品價格的總價結算

用JQuery選擇器,操作DOM元素,進行商品加減操作(動態數據可以參考ajax技術)

『柒』 js對話框,我想做一個購物車結算功能,在結算的時候會花上幾秒鍾時間,這時候

稍等一下,我貼個demo代碼給你

這個是HTML頁面

<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>Demo</title>
<scripttype="text/javascript"src="

<styletype="text/css">
body{font-size:14px;}
.btn{background:#f22d00;color:#fff;display:inline-block;width:120px;height:50px;line-height:50px;text-align:center;font-family:'LantingheiSC','MicrosoftYahei';font-size:20px;-webkit-border-radius:2px;-moz-border-radius:2px;-ms-border-radius:2px;border-radius:2px;text-decoration:none;cursor:pointer;}
.mask{background:#000;opacity:0.5;height:50px;width:120px;position:absolute;left:8px;top:8px;color:#fff;line-height:50px;text-align:center;font-weight:bold;}
.maskimg{vertical-align:-3px;margin-right:5px}
</style>
</head>
<body>
<divclass="file-box">
<formmethod="post"id="demo_form">
<ahref="javascript:;"class="btn"id="J_Go"><span>結&nbsp;算</span></a>
</form>
</div>
</body>
<scripttype="text/javascript">
$(document).ready(function(){
$('#J_Go').click(function(){
//修改結算按鈕的背景顏色
$('#J_Go').css('background','#ccc');
//添加遮罩效果
varhtml='<spanclass="mask">';
html+='<imgsrc="loading.gif">請稍候...</san>';
$('#demo_form').append(html);
//提交表單
varurl='test.php';
$.post(url,{},function(r){
if(r.status==1){
//提交表單後返回成功,則去除遮罩
$('.mask').remove();
//修改結算按鈕顏色
$('#J_Go').css('background','#f22d00');
}else{
alert(r.info);
}
},'json');
});
returnfalse;
});
</script>
</html>

這個是PHP頁面

<?php
$info['status']=1;
$info['info']='操作成功!';
exit(json_encode($info));
?>

這是效果圖示

本想上傳個附件的,一個完整的實例Demo的,但是現在才三級,不支持上傳附件。

若有任何疑問,歡迎追問!

『捌』 求JSP購物車代碼,購物車能夠實現添加、移除、刪除、結算功能,能夠實現用戶評價,生成產品銷量餅圖

JSP的這類的學習型資源目前只有畢設才滿足,所以你還是自己寫比較好,或者只有定做。要別人給你敲好的代碼一般來說是不可能的。

『玖』 js實現購物車自動結算 的功能,在網上的那個實力挺好但是下載不了啊

附件zip包里有你要的2個JS購物車。

『拾』 怎麼實現購物車結算

你好:抄這個的話,你其實很簡單的襲,你的應該做成個form,之後提交表單,獲取到對應的屬性名稱的值,然後就可以計算了,必須用post提交。也可以js算出來結果然後直接
http://lcoalhost:8080/Root?sum =500;這樣提交post請求來實現