javascript 購物車

來源:互聯網
上載者:User

var cookie={
        //讀取COOKIES,n為COOKIE名
          Get:function(n){  
            var re=new RegExp(n+'=([^;]*);?','gi');  
            var r=re.exec(document.cookie)||[];  
            return (r.length>1?r[1]:null)  
          },
            Get1:function(n){  
            var re=new RegExp(n+'=([^;]*);?','gi');  
            var r=re.exec(document.cookie)||[];  
            return unescape(r.length>1?r[1]:null)  
          },
          //寫入COOKIES,n為Cookie名,v為value
          Set:function(n,v,e,p,d,s){
            var t=new Date;  
            if(e){  
          // 8.64e7 一天  3.6e6 一小時
          t.setTime(t.getTime() + (e*3.6e6));
              
             }
            document.cookie=n+'='+v+'; '+(!e?'':'; expires='+t.toUTCString())+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+(!s?'':'; secure')  // Set cookie
          },
            Set1:function(n,v,e,p,d,s){
            var t=new Date;  
            if(e){  
          // 8.64e7 一天  3.6e6 一小時
          t.setTime(t.getTime() + (e*8.64e7));
              
             }
            document.cookie=n+'='+escape(v)+'; '+(!e?'':'; expires='+t.toUTCString())+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+(!s?'':'; secure')  // Set cookie
          },
          Del:function(n,p,d){  
            var t=cookie.Get(n);  
            document.cookie=n+'='+(!p?'':'; path='+p)+(!d?'':'; domain='+d)+'; expires=Thu, 01-Jan-70 00:00:01 GMT';  
            return t  
          }
        };

        
        
        
    var Common = {
             //移除數組中指定項
             delArr:function(ar,n) {  //n表示第幾項,從0開始算起。
             if(n<0)  //如果n<0,則不進行任何操作。
                 return ar;
             else
                 return ar.slice(0,n).concat(ar.slice(n+1,ar.length));
             },
             
             //添加至購物車
             intoCar:function(proid,quantity,proname,imgurl,_price) {
             if(proid != "" && proname != "") {
             var ProIDList = cookie.Get("carList"); //車內商品ID列表
             if(ProIDList!=null && ProIDList!="" && ProIDList!="null")
             {
                  if(!Common.hasOne(proid))
                  {
                     ProIDList += "&"+proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+escape(imgurl)+"|"+_price;
                     cookie.Set("carList",ProIDList,2,"/");//更新購物車清單
                     TotalPro = cookie.Get("TotalPro"); //當前車內含有商品的總數
                      TotalPro++; //總數+1
                     cookie.Set("TotalPro",TotalPro,2,"/");
                  }
                  else
                  {
                      alert("購物車中已含有此商品");
                  }
             }
             else {
             
                  ProIDList=proid+"="+proid+"|"+quantity+"|"+escape(proname)+"|"+escape(imgurl)+"|"+_price;
                  cookie.Set("carList",ProIDList,2,"/");//更新購物車清單
                      
                  cookie.Set("TotalPro",1,2,"/");
             }
             Common.reloadcar();//更新頂部個數顯示
             //alert(ProIDList);
             }
             
             },  //添加物品結束
             
             //重設購物車內個數
             reloadcar:function()
             {
             var t=cookie.Get("TotalPro");
             if(t!=""&&t!="null")
             document.getElementById("cart_num").innerText="(" + cookie.Get("TotalPro") + ")";
             else
             document.getElementById("cart_num").innerText="(0)";
             },  //重設結束
             
             //檢驗購物車內是否已經含有該商品
             hasOne:function(pid){
             ProIDList = cookie.Get("carList"); //車內商品ID列表
                  if(ProIDList.lastIndexOf("&") != -1){
                      var arr=ProIDList.split("&");
                      for(i=0;i<arr.length;i++)
                      {
                            var arrs=arr[i].split("|");
                          for(j=0;j<arrs.length;j++)
                          {
                              //alert(arrs[j].toString().substr(0,arrs[j].indexOf("=")));
                              if(arrs[j].toString().substr(0,arrs[j].indexOf("="))==pid)
                             {
                                 //alert(arrs[j].toString().substr(0,arrs[j].indexOf("=")) + " <--------> " + pid);
                                return true;
                             }
                          }
                      }
                  }
                  else if(ProIDList!="null"&&ProIDList!="")
                  {
                      if(ProIDList.substr(0,ProIDList.indexOf("="))==pid)
                      return true;
                  }
                  return false;
             },  //檢測結束
             
             //移除某商品
             reMoveOne:function(proid){
                  if(Common.hasOne(proid)){
                       if(ProIDList.lastIndexOf("&") != -1){
                       var arr=ProIDList.split("&");
                       for(i=0;i<arr.length;i++){
                                 var arrs=arr[i].split("|");
                              for(j=0;j<arrs.length;j++)
                              {
                                  if(arrs[j].toString().substr(0,arrs[j].indexOf("="))==proid)
                                 {
                                   var arr2=Common.delArr(arr,i);
                                   var tempStr=arr2.join("&"); //由數組重組字串
                                   
                                   cookie.Set("carList",tempStr,2,"/");//更新購物車清單
                                   var t=cookie.Get("TotalPro");
                                   cookie.Set("TotalPro",t-1,2,"/");//更新Cookies中的個數
                                  // Common.reloadcar();//更新頂部個數顯示
                                   return;
                                 }
                              }
                              }
                       }
                       else{
                           
                           cookie.Set("carList","null");//更新購物車清單
                           var t=cookie.Get("TotalPro");
                           cookie.Set("TotalPro",0,2,"/");//更新購物車清單
                          // Common.reloadcar();//更新頂部個數顯示
                       }
                  }
             
             },   //移除物品結束
             
             //修改某物品數量
             updateQuantity:function(proid,quantity){
                 ProIDList = cookie.Get("carList"); //車內商品ID列表
                 if(ProIDList.lastIndexOf("&") != -1) {
                       var arr=ProIDList.split("&");
                       var sub=Common.getSubPlace(ProIDList,proid);//擷取該物品在COOKIE數組中的下標位置
                       var arr2=arr[sub].split("|");
                       arr2[1]=quantity;
                       var tempStr=arr2.join("|");//由數組重組字串
                       arr[sub] = tempStr;
                       var newProList = arr.join("&");//由數組重組字串
                       cookie.Set("carList",newProList,2,"/");//更新購物車清單
                        //alert(newProList);
                 }
                 else {
                 
                       var arr=ProIDList.split("|");
                       arr[1]=quantity;
                       var newProList=arr.join("|");
                       cookie.Set("carList",newProList,2,"/");//更新購物車清單
                       //alert(newProList);
                 }
             
             },  //修改物品結束
             
             //返回指定物品所在數組的下標位置
             getSubPlace:function(list,proid){
                  var arr=list.split("&");
                       for(i=0;i<arr.length;i++){
                            var arrs=arr[i].split("|");
                            for(j=0;j<arrs.length;j++)
                            {
                                  if(arrs[j].toString().substr(0,arrs[j].indexOf("="))==proid)
                                 {
                                    return i;
                                 }
                              }
                       }
                     
             },
             add: function(id){
                 $(id).val(parseInt($(id).val()) + 1);
             },
             reduce: function(id){
                 if($(id).val() == 0)
                 alert("數量不能小於零!");
                 if($(id).val() > 0)
                 {
                     $(id).val($(id).val() - 1);
                 }
             }
        };
        
           //alert(Common.getSubPlace(cookie.Get("carList"),'1101'));
        
           //Common.reMoveOne('id111001');
           //alert(Common.hasOne('id111001'));

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.