首頁  >  文章  >  web前端  >  購物車程式完善版(JavaScript程式碼)

購物車程式完善版(JavaScript程式碼)

高洛峰
高洛峰原創
2016-11-26 10:13:401373瀏覽

[javascript]
/********************************購物車類*************** ************************/ 
 
    var Product=function(productArr){/*商品類基類productArr=>array;productArr['ID'],productArrname'srr=>array;productArr['ID'],productArrname'],productArr] 'pic'],=productArr['lvs'],productArr['寬度'],productArr['厚度'],productArr['長度'],productArr['開度'],productArr['容量']*/
        var _ID=productArr['ID'],_name=productArr['name'],_pic=productArr['pic'],_price=productArr['price'],_weight=productArrorweywion ['原價'],_this=this; 
        this.num=productArr['num']; 
        this.getName=function(){ 
            return _name; 
            } 
        this.getID=function(){ 
      
            }    
        this.getPrice=function(){ 
 rice; 
            } 
        this.getWeight=function(){ 
     
            } 
        this.getPic=function(){ 
     
            } 
        this.getOriginalprice=function(){ 
    
            } 
        this.setNum=function(newNum){ 
.        
            } 
        this.getTotal=function(){ 
     
            回報總計; 
            }     
    var makeProductObj=function(gID,pID,num){//製造產品物件函數,傳回製造的產品物件,gID=>資料群組ID, pID=>商品ID
    ** *******製造代碼*********/ 
            var pData=[]; 
(函數(gid,pid){
$ .ajax({{
url:'/module/data/default.php?action = datadEtail&groupid&groupid ='+gid+'+gid+'&id ='+pid+'+pid&groupid ='+gid+'+gid+'&id ='+pid+'+pid+groupid ='+gid+'+gid+'&id ='+pid+'+pid+groupid ='+gid+'+gid+'&id ='+pid+'+pid+groupid ='+gid+'+gid+'&id ='+pid+'+pid+>groupid'&r ='+newdate(),
                    type:'post', 
                    async:false, 
                      success:function(dataXML){ 
                     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(dataXML).children('root').children('extend17').text(); 
                        } 
          
                })(gID,pID); 
            返回新產品({ 
                num:num, 
               name:pData.name, 
                ID:pID, 
                價格:pData.價格, 
                圖片:pData.pic, 
                重量:pData.weight, 
                原始價格:pData.原價, 
                }); 
        } 
    var cartCookie=$.cookies.get('productItem');//cookie的全域變數=>'商品ID|ookie,商品ID| 
     
    var ProductCollection=function(){//商品商品類 
            var products=[]; 
            var hasOne=function(productObj){ 
              
                for(var i=0;i      ){ 
                        isOne=1; }
                        休息中; 
                      } 
               且返回 isOne; 
                } 
                                   //alert(mockCookie); 
              if(cartCookie){ 
                
                 for(var i=0;i                   var _productTemp=initProduct[i].split('|'); 
                   var _proObj=new makeProductObj(4,parseInt(_productTemp[0]),parseInt(_productTemp[1]))[0]),parseInt(_productTemp[1]))[0]),parseInt(_productTemp[1])); 
                   if(hasOne(_proObj)!=1){ 
        
                   } 
                   } 
            /*******初始哈products數組代碼*********/ 
           ctObj){//添加商品 
                   if(hasOne(productObj)!=1){   products.push(productObj); 
                     } 
           productObj){//刪除商品 
                var _tempProducts=[]; 
                for(var i=0;i      )){ 
                        //console.log(產品[i] ] 。 
                       _tempProducts.push(products[i]); 
                        } 
                  產品=_temp產品; 
                }//del 
                  for(var k=0;k                 ==pID){ 
                           作用與產品[k].setNum(newNum); 
                            休息; 
                          } 
                       } 
               } 
            this.getProductById=function(pID){//pID為商品ID,獲取商品對象
for(var j=0;j                                        return products[j]; 
                                           return false;
                } 
            this.getProdu  turn products; 
                } 
               var _needMoney=0; 
                for(var k=0;k                        } 
                return _needMoney; 
        
         
     
    var cart=function(){//購物車類,productCollection為委託對象 
            var _productCollection=''; 
       var _this=this; 
            this.setCollection=function(collection){//設定collection 
              _nowCollection= _productCollection;//動態collection對象 
               _this.setTemp(); 
       this.temp=[];//初始化collection物件 
            this.setTemp=function(){ 
                           throw new Error( '請先指定collection對象!'); 
                        } .getProducts(); 
                    for(var i=0;i_this.temp[i]=[]; 
                                   } 
                }; 
      
                   false){ 
                       _nowCollection.delProduct(needProduct);   } 
                } 
           的數量,pID->商品ID 
                    _nowCollection.setNumBy            this.getCurrentNumByPid=function(pID){//取得某商品的數量,pID->商品ID 
      getProductById(pID); 
                    return productObj.num;       this.getTotalMoney=function(){//取得總金額 
                            return _money; 
                } 
    pID){//取得單一商品的總金額 
                    var _needProduct=     if(_needProduct===false){ 
                                } 
                    var _totalMoney= _needProduct.getTotal(); 
                    return _totalMoney; 
                } 
            this.renderCart=function(){//渲染函數 
                    throw new Error('錯誤,該方法必須在子類中實現'); 
                } 
            this.bindEvent =function(){//綁定事件函數 
健康 new Error('錯誤,此方法必須在子類別中實現'); 
                } 
       this.touchOff=function(){//觸發 www.2cto.com  
                   _this.renderCart (); 
                    _this.bindEvent(); 
      this.getNewCollection=function(){//獲得新的collection對象 
                        } 
/********************************購物車類***************** ***********************/ 
        var shoppingTrolley=function (){ 
                //alert('test'); 
           }    if(!cartTest){ 
                    throw new    
                /***************檢查*****************/ 
                cartTest.renderCart=function(){//子渲染 
    _content=''; 
                           針對(var i=0) var _productEach=this.getNewCollection().getProducts()[i]; 
                   ; 
                                     _content+='

'+_productEach.getName()+' $'+_productEach.getOriginalprice()+' $' +_productEach.getPrice()+' '+_productEach.getWeight()+'公斤 - '+_productEach.num+' + $'+_productEach。 ="red">刪除 ;'; 
                                   //document.write(_content); 
                            //alert(_content); 
                             $('#checkCart').find                 $('.step_total').find('.step_total_right').children ( ' span').html('$'+cartTest.getTotalMoney()); 
                            /**彈出框計數**/ 
                  }//renderCart 
                    cartTest.bindEvent=function(){ 
                                    var _this=this; 
                                地 
                                                                                                                    _each.find('td:last').css('cursor','pointer').click(function(){ 
                   //alert('刪除'); 
                          ); 
                                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( )); 
                                                                                                    
               _each.children('td[class=numJia]').css('cur sor' ,'指標').click (函數(){ 
                                                var _jiaThis=$(this); 
                                                     //alert($(this).parents('tr').parents('table').find('input[type=hidden]').值()); 
                                                    _this.setNumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val(),_this.getCurrentNumByPid($(this). Parent('tr').find('td:first').find('input[type=hidden]').val())+1); //
                                  .siblings('td[class=nowNum]').children('input').val()); 
                                  ').html(
                                        _this.getCurrentNumByPid(_jiaThis.parents('tr').find('td:first').find('input[type=hidden]' .val()) 
                               
                                                                     ').html('$'+_this.getTotalMoneyByPid(_jiaThis.parents('tr'). find('td:first').find('input[type=hidden]').val())); 
/**&#&* /
/** /**&&* /
$('。step_total')。 ; 
                                                                                  });//點擊增加商品數量 
                                           _each.children('td[class=numJian]').css('cursor','pointer').click(function(){ 
                                                    var _jianThis=$(this);
                                  ).parents('tr').find('td:first').find('input[type=hidden]').val())>1){ 
_這個.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( 
                               NumByPid($(this).parents('tr').find('td:first').find('input[type=hidden]').val()) 
                       );           );            / * **********重新計算某個商品的總金額***********/ 
                              eachTotal]').children('span').html('$'+_this.getTotalMoneyByPid(_jianThis.parents('tr').找到( ' td:first').find('input[type=hidden]').val())); 
                                                                          
/*******重新計算商品的總金額*******/ 
                             total').find('.step_total_right').children('span').html('$'+_this.getTotalMoney()); 
                             /*******重新計算商品的總金額*******/ 
                                若目前商品數量大於1 
                                                                             }//bindEvent 
                        
                    $( window).unload(function(){//離開頁面時 
                                       var lastCollection=cartTest.getNewCollection();  
        ection.getProducts(); 
                            //console .log(_products.length); 
                                       var _countNum=0; 
                                                    '|'+_products[i].num+','; 
                         Weight(); 
                                                           _cartCookie=_cartCookie.substring(0,_cartCookie.length-1); 
                            //alert(_cartCookie); 
                            //alert(_cuntNum); 
                            //alert(_countNum); 
                            $.cookies.set('productItem             $.cookies.set('countNum',_countNum);//設定cookie的數量 
           
                         
                      $('#btn').click(function(){ 
                           /******************若未選擇任何商品返回*****************/ 
                            var _listProducts=cartTest.getNewCollection().getProducts(); 
                          if(_listProducts.length                             }                                            window.location.href="http://www.php1.  
                 
            }  
    var ProductCollections= new ProductCollection(); 
    var cartTest=new cart(); 
    cartTest.setCollection(productCollections); 
   新的購物車(); 
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn