基于JQuery實(shí)現(xiàn)的類似購物商城的購物車
更新時(shí)間:2011年12月06日 19:38:39 作者:
使用JQuery Clone 模板來實(shí)現(xiàn)商品信息的展示,展現(xiàn)形式可以通過修改模板中的td來確定每一行顯示多少個(gè)商品信息
商品信息使用JSON數(shù)據(jù)來模擬
同一個(gè)產(chǎn)品點(diǎn)擊多次,不會(huì)重復(fù)添加,而是在已有的基礎(chǔ)上數(shù)量+1,
商品數(shù)量也可以手動(dòng)輸入,當(dāng)輸入0時(shí),該商品將自動(dòng)從購物車刪除(點(diǎn)擊減號(hào)到小于1時(shí),也會(huì)提示是否從購物車刪除商品信息)
每個(gè)產(chǎn)品的價(jià)格和總價(jià)都會(huì)根據(jù)添加和刪除的操作來動(dòng)態(tài)計(jì)算
附下載鏈接:/201112/yuanma/jquery_gouwuche.rar
基本的功能都已經(jīng)實(shí)現(xiàn), 建議使用IE瀏覽器運(yùn)行,其他瀏覽器沒有測(cè)試

HTML代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<h2 style="text-align:left">商品列表</h2>
<table width="800" border="0" id="tblProduct" cellpadding="1" cellspacing="1" bgcolor="black">
<tr id="trMaster" bgcolor="white" style="display:none">
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
</tr>
</table>
<h2 style="text-align:left">購物車</h2>
<table width="800" border="0" id="tblOrder" cellpadding="1" cellspacing="1" bgcolor="black">
<tr bgcolor="white"><td>序號(hào)</td><td>編號(hào)</td><td>名稱</td><td>描述</td><td>數(shù)量</td><td>單價(jià)</td><td>總價(jià)</td><td>刪除</td></tr>
<tr id="orderMarter" bgcolor="white" style="display:none">
<td style=" width:5%">1</td>
<td style=" width:10%">000001</td>
<td style=" width:10%">aa</td>
<td>aaaaa</td>
<td style=" width:15%">
<input type="text" name="txtNum" style="width:50px;" value="0" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/>
<input type="button" name='btnAdd' style="width:15px; text-align:center" value="+" />
<input type="button" name='btnCut' style="width:15px; text-align:center" value="-" />
</td>
<td style=" width:10%">0</td>
<td style=" width:10%"><font color="red">0</font></td>
<td style=" width:5%; text-align:center"><input type="button" name='btnRemove' style="width:30px; text-align:center" value="X" /></td>
</tr>
</table>
<table width="800" border="0" id="tblTotal" cellpadding="1" cellspacing="1" bgcolor="black">
<tr bgcolor="white" align="right">
<td>總價(jià)</td>
<td id="tdTotal"><font color="#FF0000" size="+1" face="Arial, Helvetica, sans-serif">0.0</font></td>
</tr>
</table>
</body>
JS 代碼
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
/*
@@
@@
@@
*/
var product=[
{prodId:"000001",prodName:"aa",price:2.5,description:"aaaaa"},
{prodId:"000002",prodName:"bb",price:2.4,description:"bbbbb"},
{prodId:"000003",prodName:"cc",price:2.3,description:"ccccc"},
{prodId:"000004",prodName:"dd",price:2.2,description:"ddddd"},
{prodId:"000005",prodName:"ee",price:2.1,description:"eeeee"},
{prodId:"000006",prodName:"ff",price:2.0,description:"fffff"},
{prodId:"000007",prodName:"gg",price:1.9,description:"ggggg"},
{prodId:"000008",prodName:"hh",price:1.8,description:"hhhhh"},
{prodId:"000009",prodName:"ii",price:1.7,description:"iiiii"}
];
</script>
<script type="text/javascript">
var j=0,i=-1,k=-1,len=$("#trMaster td").size();//數(shù)據(jù)和模板的td 都可以自定義,格式正確即可
$.each(product,function(index,prod){
i++;
k++;
if(i%len==0){
j++;
CloneTR(j);//根據(jù)模板來克隆行
}
if(k==len)
k=0;
setTD(j,k,prod.prodId,prod.prodName,prod.price,prod.description);//給克隆行的td 賦值
});
//根據(jù)模板來克隆行
function CloneTR(id){
$("#trMaster").clone(true).css("display","block").attr("id","tr"+id).appendTo("#tblProduct");
$("#tr"+id+" td span").empty();
}
//給克隆行的td 賦值
function setTD(trId,index,prodId,prodName,price,description){
var tr=$("#tr"+trId);
$(tr).find("td:eq("+index+")").find("span:eq(0)").html(prodId);
$(tr).find("td:eq("+index+")").find("span:eq(1)").html(prodName);
$(tr).find("td:eq("+index+")").find("span:eq(2)").html(price);
$(tr).find("td:eq("+index+")").find("span:eq(3)").html(description);
}
var tempId="";
var num=0;
//點(diǎn)擊buy
$("input[name='btnBuy']").click(function(){
var elem=$(this).parent().parent();//獲取點(diǎn)擊的button 的td
var prodId=$(elem).find("span:eq(0)").html();
var prodName=$(elem).find("span:eq(1)").html();
var price=$(elem).find("span:eq(2)").html();
var description=$(elem).find("span:eq(3)").html();
if(prodId==""||prodId==null||prodId==undefined){
alert("請(qǐng)點(diǎn)擊其他產(chǎn)品");
}else{
if(tempId.indexOf(prodId)!=-1){
if(confirm('已存在,確定數(shù)量+1 嗎?')){
$("#tblOrder tr:gt(1)").each(function(){
if($(this).find("td:eq(1)").html()==prodId){
var num=$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value");
$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value",+num+1);//相當(dāng)于parseInt(num)+1;
}
});
}
}else{
num++;
CloneOrder(num,prodId,prodName,price,description);
}
tempId+=prodId+",";
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
//根據(jù)訂單模板來clone 訂單
function CloneOrder(id,prodId,prodName,price,description){
$("#orderMarter").clone(true).css("display","block").attr("id","tro"+id).appendTo("#tblOrder");
var tr=$("#tro"+id);
$(tr).find("td:eq(0)").html(num);
$(tr).find("td:eq(1)").html(prodId);
$(tr).find("td:eq(2)").html(prodName);
$(tr).find("td:eq(3)").html(description);
$(tr).find("td:eq(4)").find("input[name='txtNum']").attr("value","1");
$(tr).find("td:eq(5)").html(price);
$(tr).find("td:eq(6)").html("<font color='red'>"+price+"</font>");
}
//獲取總共價(jià)格
function GetTotalPrice(){
var totalNum=0;
$("#tblOrder tr:gt(1)").each(function(){
var value=parseFloat($(this).find("td:eq(6)").text());
totalNum+=value;
});
return totalNum;
}
$(function(){
$("#tblOrder input[name='txtNum']").bind("propertychange",function(){
var value=$(this).val();
var tr=$(this).parent().parent();
if(value==0){
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
}
}else{
var price=$(tr).find("td:eq(5)").html();
var total=value*price;
$(tr).find("td:eq(6)").html("<font color='red'>"+total.toFixed(2)+"</font>");
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
//加1
$("#tblOrder input[name='btnAdd']").click(function(){
var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
var value=$(txtBox).attr("value");
value=+value+1;
$(txtBox).attr("value",value)
});
//減1
$("#tblOrder input[name='btnCut']").click(function(){
var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
var tr=$(this).parent().parent();
var value=$(txtBox).attr("value");
if(value>1){
value=+value-1;
$(txtBox).attr("value",value)
}else{
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
}
});
//刪除btnRemove
$("#tblOrder input[name='btnRemove']").click(function(){
var tr=$(this).parent().parent();
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
});
</script>
同一個(gè)產(chǎn)品點(diǎn)擊多次,不會(huì)重復(fù)添加,而是在已有的基礎(chǔ)上數(shù)量+1,
商品數(shù)量也可以手動(dòng)輸入,當(dāng)輸入0時(shí),該商品將自動(dòng)從購物車刪除(點(diǎn)擊減號(hào)到小于1時(shí),也會(huì)提示是否從購物車刪除商品信息)
每個(gè)產(chǎn)品的價(jià)格和總價(jià)都會(huì)根據(jù)添加和刪除的操作來動(dòng)態(tài)計(jì)算
附下載鏈接:/201112/yuanma/jquery_gouwuche.rar
基本的功能都已經(jīng)實(shí)現(xiàn), 建議使用IE瀏覽器運(yùn)行,其他瀏覽器沒有測(cè)試

HTML代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
</head>
<body>
<h2 style="text-align:left">商品列表</h2>
<table width="800" border="0" id="tblProduct" cellpadding="1" cellspacing="1" bgcolor="black">
<tr id="trMaster" bgcolor="white" style="display:none">
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
<td>
編號(hào):<span>000001</span><br />
名稱:<span>aa</span><br />
價(jià)格:<span>2.5</span><br />
描述:<span>aaaaa</span><br />
<div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
</td>
</tr>
</table>
<h2 style="text-align:left">購物車</h2>
<table width="800" border="0" id="tblOrder" cellpadding="1" cellspacing="1" bgcolor="black">
<tr bgcolor="white"><td>序號(hào)</td><td>編號(hào)</td><td>名稱</td><td>描述</td><td>數(shù)量</td><td>單價(jià)</td><td>總價(jià)</td><td>刪除</td></tr>
<tr id="orderMarter" bgcolor="white" style="display:none">
<td style=" width:5%">1</td>
<td style=" width:10%">000001</td>
<td style=" width:10%">aa</td>
<td>aaaaa</td>
<td style=" width:15%">
<input type="text" name="txtNum" style="width:50px;" value="0" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/>
<input type="button" name='btnAdd' style="width:15px; text-align:center" value="+" />
<input type="button" name='btnCut' style="width:15px; text-align:center" value="-" />
</td>
<td style=" width:10%">0</td>
<td style=" width:10%"><font color="red">0</font></td>
<td style=" width:5%; text-align:center"><input type="button" name='btnRemove' style="width:30px; text-align:center" value="X" /></td>
</tr>
</table>
<table width="800" border="0" id="tblTotal" cellpadding="1" cellspacing="1" bgcolor="black">
<tr bgcolor="white" align="right">
<td>總價(jià)</td>
<td id="tdTotal"><font color="#FF0000" size="+1" face="Arial, Helvetica, sans-serif">0.0</font></td>
</tr>
</table>
</body>
JS 代碼
復(fù)制代碼 代碼如下:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
/*
@@
@@
@@
*/
var product=[
{prodId:"000001",prodName:"aa",price:2.5,description:"aaaaa"},
{prodId:"000002",prodName:"bb",price:2.4,description:"bbbbb"},
{prodId:"000003",prodName:"cc",price:2.3,description:"ccccc"},
{prodId:"000004",prodName:"dd",price:2.2,description:"ddddd"},
{prodId:"000005",prodName:"ee",price:2.1,description:"eeeee"},
{prodId:"000006",prodName:"ff",price:2.0,description:"fffff"},
{prodId:"000007",prodName:"gg",price:1.9,description:"ggggg"},
{prodId:"000008",prodName:"hh",price:1.8,description:"hhhhh"},
{prodId:"000009",prodName:"ii",price:1.7,description:"iiiii"}
];
</script>
<script type="text/javascript">
var j=0,i=-1,k=-1,len=$("#trMaster td").size();//數(shù)據(jù)和模板的td 都可以自定義,格式正確即可
$.each(product,function(index,prod){
i++;
k++;
if(i%len==0){
j++;
CloneTR(j);//根據(jù)模板來克隆行
}
if(k==len)
k=0;
setTD(j,k,prod.prodId,prod.prodName,prod.price,prod.description);//給克隆行的td 賦值
});
//根據(jù)模板來克隆行
function CloneTR(id){
$("#trMaster").clone(true).css("display","block").attr("id","tr"+id).appendTo("#tblProduct");
$("#tr"+id+" td span").empty();
}
//給克隆行的td 賦值
function setTD(trId,index,prodId,prodName,price,description){
var tr=$("#tr"+trId);
$(tr).find("td:eq("+index+")").find("span:eq(0)").html(prodId);
$(tr).find("td:eq("+index+")").find("span:eq(1)").html(prodName);
$(tr).find("td:eq("+index+")").find("span:eq(2)").html(price);
$(tr).find("td:eq("+index+")").find("span:eq(3)").html(description);
}
var tempId="";
var num=0;
//點(diǎn)擊buy
$("input[name='btnBuy']").click(function(){
var elem=$(this).parent().parent();//獲取點(diǎn)擊的button 的td
var prodId=$(elem).find("span:eq(0)").html();
var prodName=$(elem).find("span:eq(1)").html();
var price=$(elem).find("span:eq(2)").html();
var description=$(elem).find("span:eq(3)").html();
if(prodId==""||prodId==null||prodId==undefined){
alert("請(qǐng)點(diǎn)擊其他產(chǎn)品");
}else{
if(tempId.indexOf(prodId)!=-1){
if(confirm('已存在,確定數(shù)量+1 嗎?')){
$("#tblOrder tr:gt(1)").each(function(){
if($(this).find("td:eq(1)").html()==prodId){
var num=$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value");
$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value",+num+1);//相當(dāng)于parseInt(num)+1;
}
});
}
}else{
num++;
CloneOrder(num,prodId,prodName,price,description);
}
tempId+=prodId+",";
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
//根據(jù)訂單模板來clone 訂單
function CloneOrder(id,prodId,prodName,price,description){
$("#orderMarter").clone(true).css("display","block").attr("id","tro"+id).appendTo("#tblOrder");
var tr=$("#tro"+id);
$(tr).find("td:eq(0)").html(num);
$(tr).find("td:eq(1)").html(prodId);
$(tr).find("td:eq(2)").html(prodName);
$(tr).find("td:eq(3)").html(description);
$(tr).find("td:eq(4)").find("input[name='txtNum']").attr("value","1");
$(tr).find("td:eq(5)").html(price);
$(tr).find("td:eq(6)").html("<font color='red'>"+price+"</font>");
}
//獲取總共價(jià)格
function GetTotalPrice(){
var totalNum=0;
$("#tblOrder tr:gt(1)").each(function(){
var value=parseFloat($(this).find("td:eq(6)").text());
totalNum+=value;
});
return totalNum;
}
$(function(){
$("#tblOrder input[name='txtNum']").bind("propertychange",function(){
var value=$(this).val();
var tr=$(this).parent().parent();
if(value==0){
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
}
}else{
var price=$(tr).find("td:eq(5)").html();
var total=value*price;
$(tr).find("td:eq(6)").html("<font color='red'>"+total.toFixed(2)+"</font>");
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
//加1
$("#tblOrder input[name='btnAdd']").click(function(){
var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
var value=$(txtBox).attr("value");
value=+value+1;
$(txtBox).attr("value",value)
});
//減1
$("#tblOrder input[name='btnCut']").click(function(){
var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
var tr=$(this).parent().parent();
var value=$(txtBox).attr("value");
if(value>1){
value=+value-1;
$(txtBox).attr("value",value)
}else{
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
}
});
//刪除btnRemove
$("#tblOrder input[name='btnRemove']").click(function(){
var tr=$(this).parent().parent();
if(confirm('確定要?jiǎng)h除該商品嗎?')){
$(tr).remove();
$("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
}
});
});
</script>
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)購物車多物品數(shù)量的加減+總價(jià)計(jì)算
- jQuery實(shí)現(xiàn)加入購物車飛入動(dòng)畫效果
- JQuery實(shí)現(xiàn)的購物車功能(可以減少或者添加商品并自動(dòng)計(jì)算價(jià)格)
- jQuery實(shí)現(xiàn)購物車計(jì)算價(jià)格功能的方法
- 純jquery實(shí)現(xiàn)模仿淘寶購物車結(jié)算
- jQuery實(shí)現(xiàn)購物車數(shù)字加減效果
- jQuery+HTML5加入購物車代碼分享
- jquery實(shí)現(xiàn)購物車基本功能
- jquery購物車結(jié)算功能實(shí)現(xiàn)方法
- jQuery實(shí)現(xiàn)購物車
相關(guān)文章
jQuery 如何實(shí)現(xiàn)一個(gè)滑動(dòng)按鈕開關(guān)
本文給大家分享一段jquery代碼實(shí)現(xiàn)滑動(dòng)按鈕開關(guān)的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的的朋友參考下2016-12-12
基于jQuery 實(shí)現(xiàn)bootstrapValidator下的全局驗(yàn)證
這篇文章主要介紹了基于jQuery 實(shí)現(xiàn)bootstrapValidator下的全局驗(yàn)證 的相關(guān)資料,需要的朋友可以參考下2015-12-12
jquery lazyload延遲加載技術(shù)的實(shí)現(xiàn)原理分析
懶加載技術(shù)(簡(jiǎn)稱lazyload)并不是新技術(shù),它是js程序員對(duì)網(wǎng)頁性能優(yōu)化的一種方案。lazyload的核心是按需加載。在大型網(wǎng)站中都有l(wèi)azyload的身影,例如谷歌的圖片搜索頁,迅雷首頁,淘寶網(wǎng),QQ空間等。2011-01-01
基于jquery的滾動(dòng)鼠標(biāo)放大縮小圖片效果
基于jquery的滾動(dòng)鼠標(biāo)放大縮小圖片效果,需要的朋友可以參考下。2011-10-10
jQuery代碼實(shí)現(xiàn)圖片墻自動(dòng)+手動(dòng)淡入淡出切換效果
在網(wǎng)頁上經(jīng)??梢钥吹接斜尘皥D片可以自動(dòng)淡入淡入切換的效果,非常漂亮,實(shí)用性也非常高,今天小編給大家分享基于jquery代碼實(shí)現(xiàn)圖片墻自動(dòng)+手動(dòng)淡入淡出切換效果,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
JQuery 引發(fā)兩次$(document.ready)事件
ASP.net MVC 做了個(gè)工程,不知道為什么Search按就總是執(zhí)行兩次。2010-01-01
jQuery判斷多個(gè)input file 都不能為空的例子
這篇文章主要介紹了jQuery判斷多個(gè)input file 都不能為空的例子,即一次性判斷多個(gè)上傳文件選擇框不能為空的方法,需要的朋友可以參考下2015-06-06

