ホームページ  >  記事  >  ウェブフロントエンド  >  ショッピングカートプログラムの完全版(JavaScriptコード)

ショッピングカートプログラムの完全版(JavaScriptコード)

高洛峰
高洛峰オリジナル
2016-11-26 10:13:401377ブラウズ

[javascript]
/***********************************ショッピングカートカテゴリー************** **** ************************/

var product=function(productArr){/*商品类基类productArr=>array;productArr['ID'],productArr['name'],productArr ['pic'],=productArr['lvs'],productArr['width'],productArr['thickness'],productArr['length'],productArr['open'],productArr['capacity']*/
var _ID=productArr['ID'],_name=productArr['name'],_pic=productArr['pic'],_price=productArr['price'],_weight=productArr['weight'],_originalPrice=productArr ['オリジナル価格'],_this=this; 
this.num=productArr['num']; 
this.getName=function(){
return _name; 
}
this.getID=function(){
return _ID; 
}
this.getPrice=function(){
return _price; 
}
this.getWeight=function(){
return _weight; 
}
this.getPic=function(){
return _pic; 
}
this.getOriginalprice=function(){
return _originalPrice; 
}
this.setNum=function(newNum){
_this.num=newNum; 
}
this.getTotal=function(){
var total=_this.num*_this.getPrice(); 
合計を返します。 
}
}}

var makeproductobj = function(gid、pid、num){//制造product对象、返回制造的gid =>数据组id、pid => *******製造コード*********/
var pData=[]; 
(function(gID,pID){
$.ajax({
url:'/module/data/default.php?action=dataDetail&groupID='+gID+'&ID='+pID+'&r='+newD食べた()、
type:'post',
dataType:'xml',
async:false,
/**工場出荷時の方法*** /
success:function(dataXML){
pData.price=$(dataXML).children('root ').children('extend19').text();
                        pData.originalPrice=$(dataXML).children('root').children('extend9').text(); 
pData.weight=$(dataXML).children('root').children('extend21').text(); 
pData.name=$(dataXML).children('root').children('name').text(); 
pData.pic=$(dataXML).children('root').children('extend17').text(); 
}
}); 
})(gID,pID); 
新しい製品を返します({
num:num,
name:pData.name,
ID:pID,
price:pData.価格,
pic:pData.pic,
weight:pData.weight,
originalPrice:pData.originalPrice ,
}); 
}
var cartCookie=$.cookies.get('productItem');//cookieの全局变量=>'商品ID|数量,商品ID|数量'
//alert(mockCookie); 

var productCollection=function(){//商品搜集类
var products=[]; 
var hasOne=function(productObj){
var isOne=0; 
for(var i=0;i if(products[i].getID()==productObj.getID()){
isOne=1; 
休憩。 
}
}
戻り値は One; 
}
var initProduct
/********初期製品配列コード*************/
//alert(mockCookie); 
if(cartCookie){
initProduct=cartCookie.split(','); 
for(var i=0;i                   var _productTemp=initProduct[i].split('|'); 
var _proObj=new makeProductObj(4,parseInt(_productTemp[0]),parseInt(_productTemp[1])); 
if(hasOne(_proObj)!=1){
products.push(_proObj); 
}
}
}
/*******初期製品配列コード***********/

this.addProduct=function(productObj){//追加商品
if(hasOne(productObj)!=1){
products.push(productObj); 
}
}
this.delProduct=function(productObj){//删除商品
var _tempProducts=[]; 
for(var i=0;i if(products[i].getID()!==productObj.getID()){
//コンソール。 log(製品[i]. getID()); 
_tempProducts.push(products[i]); 
}
}
products=_tempProducts; 
}//del
this.setNumByPid=function(pID,newNum){//设置商品数
for(var k=0;k if(products[k].getID() ==pID){
product[k].setNum(newNum); 
休憩。 
}
}
}
this.getProductById=function(pID){//pID ,获得商品对象
for(var j = 0; j< length; j ++){//すべての商品の合計価格を取得します。 ; lutss.length {getneedmoneyショッピングクラス、delegate ofbers var _nowcollection = '; );
}

if(!_productCollection) use ‐ '最初にコレクション オブジェクトを指定してください!' ;i++){
_this.temp[i]=[];
This.delProduct=function(pID){//単一の製品 pID->製品番号の ID を削除します。
var needProduct=_nowCollection.getProductById(pID);
製品) ; アイテム数、pID-> 製品 ID
pID-> 製品 ID
var productObj =_nowCollection.getProductById(pID);
return productObj.num; var _money=_nowCollection.getNeedMoney(); out out =function( pID){//1 つの商品の合計金額を取得します
throw new Error('そのような商品はありません');
var _totalMoney=_needProduct.getTotal();
新規を投げるError('エラー、このメソッドはサブクラス'); this.bindEvent=function(){//バインド イベント関数
throw new Error('エラー、このメソッドはサブクラスで実装する必要があります'); _this.renderCart ();
_this.bindEvent(); コレクション;
//alert(' '); オブジェクト'); #&*/
;this.getNewCollection().getProducts().length;i++){
var _productEach=this.getNewCollection().getProducts()[i];                                     _content+=''+_productEach.getName()+'$'+_productEach.getOriginalprice()+'$'+_productEach.getPrice()+''+_productEach.getWeight()+'Kg-<スパン クラス="red">'+_productEach.num+'+$'+_productEach。 getTotal()+'削除 }
//document.write(_content); 
//alert(_content); 
$('#checkCart').find('tr').eq(0).after(_content);//渲染面板
$('.step_total').find('.step_total_そうですね').children('スパン').html('$'+cartTest.getTotalMoney()); 
/**ポップボックス数**/
}//renderCart
cartTest.bindEvent=function(){
var _this=this; 
//alert($('#'+win1.ID).find('.cartList').eq(0).html()); 
$('#checkCart').find('.cartList').each(function(index){
_each=$ (これ);
                                            _each.find('td:last').css('cursor','pointer').click(function(){
//alert('删除');
var _tempThis=$(this);
_this。 delProduct($(this).parents('tr').find('td:first').find('input[type=hidden]').val());
$(this).parents('tr ').remove();
/*******商品の合計金額を再計算します*******/
$('.step_total').find('.step_total_right').children('span').html('$'+_this.getTotalMoney( ));
/*******商品の合計金額を再計算します*******/
});/ /click删除商品

_each.children('td[class=numJia]')。 css('カーソル','ポインタ').click( function(){
var _jiaThis=$(this);
//alert($(this).parents('tr').parents('table').find('input[type=hidden]').val ()); 
                                                    _this.setNumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val(),_this.getCurrentNumByPid($(this). parents('tr').find('td:first').find('input[type=hidden]').val())+1); 
//alert(_jiaThis.parents('td').siblings('td[class=nowNum]').children('input').val()); 
_jiaThis.siblings('td[class=nowNum]').html(
_this.getCurrentNumByPid(_jiaThis.parents('tr').find('td:first').find('input[type=hidden]' ).val())
); _ji aThis.siblings('td[class=eachTotal]').children('span').html('$'+_this.getTotalMoneyByPid(_jiaThis.parents('tr'). find('td:first').find('input[type=hidden]').val())); 
/**&#&* /
/**&#&* /
$( '。STEP_TOTAL')。find( '。STEP_TOTAL_RIGHT')。子供( 'SPAN')。html( '$'+_ this .getTotalMoney()); 
/***********特定の商品の合計金額を再計算します***********/
                                                });//click增加商品数量
_each.children('td[class=numJian]').css('cursor','pointer').click(function(){
var _jianThis=$(this);
if(_this.getCurrentNumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val())>1){
_this.setNumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val(),_this.getCurrentNumByPid($(this). parents('tr').find('td:first').find('input[type=hidden]').val())-1);
_jianThis.siblings('td[class=nowNum] ') .html(
_this.getCurrentNumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val()) / ***********特定の商品の合計金額を再計算します***********/
_jianThis.siblings('td[class=eachTotal]').children('span').html('$'+_this.getTotalMoneyByPid(_jianThis.parents('tr').見つける( 'td:first').find('input[type=hidden]').val())); 
/***********特定の商品の合計金額を再計算します*************/

/**&#&*/**&#&*/
carttest.touchoff();犬の頭, "; tproducts ();
// コンソール .log(_products.length) for(var i=0;i<_products.length;i++){
'|'+_products[i].num+' 、';

                            _cartCookie=_cartCookie.substring(0,_cartCookie.length-1); 
//alert(_cartCookie); 
//alert(_cuntNum); 
//alert(_countNum); 
$.cookies.set('productItem',_cartCookie);//設置cookie
$.cookies.set('countNum',_countNum);//設置量のcookie
}); 

$('#btn').click(function(){
/******************製品が選択されていない場合は、****************** に戻ります*/
var _listProducts=cartTest.getNewCollection().getProducts();
if(_listProducts.length<0||_listProducts .length==0){
alert('商品を選択してください!');
return;
}
window.location.href="http://www.php1.cn/"> }); 

}
/***********************ショッピングカートカテゴリー************************** **** ********/

/******************インスタンス化されたオブジェクト*******************/
var productCollections= new productCollection(); 
var cartTest=新しいカート(); 
カートTest.setCollection(productCollections); 
new shoppingTrolley(); 

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。