成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? jQuery基于json與cookie實現購物車的方法
全站頭部文字 我要出現在這里
jQuery基于json與cookie實現購物車的方法 625 2024-03-07   

本文實例講述了jQuery基于json與cookie實現購物車的方法。分享給大家供大家參考,具體如下:

json 格式:

[{'ProductID':ABC','Num':'1'},{'ProductID':DEF,'Num':'2'}]

這里使用到了 $.cookie這個插件。這個插件的代碼在文章的最后

/*
添加商品及數量到購物車cookie中,返回當前商品在cookie中的總數
*/
function AddToShoppingCar(id, num) {
  var _num = 1;
  if (num != undefined)
    _num = num;
  var totalNum = _num; //總數默認為傳入參數
  var cookieSet = { expires: 7, path: '/' }; //設置cookie路徑的
//  $.cookie(cookieProductID, null, cookieSet);//清除Cookie
  var jsonStr = "[{'ProductID':'" + id + "','Num':'" + _num + "'}]"; //構造json字符串,id是商品id  num是這個商品的數量
  if ($.cookie(cookieProductID) == null) {
    $.cookie(cookieProductID, jsonStr, cookieSet); //如果沒有這個cookie就設置他
  }
  else {
    var jsonObj = eval('(' + $.cookie(cookieProductID) + ')'); //如果有,把json字符串轉換成對象
    var findProduct = false;//是否找到產品ID,找到則為TRUE,否則為FALSH
    for (var obj in jsonObj) {
      if (jsonObj[obj].ProductID == id) {
        jsonObj[obj].Num = Number(jsonObj[obj].Num) + _num;
        totalNum = jsonObj[obj].Num;
        findProduct = true;
        break;
      }
    }
    if (findProduct == false) { //沒找到,則添加
      jsonObj[jsonObj.length] = new Object();
      jsonObj[jsonObj.length - 1].ProductID = id;
      jsonObj[jsonObj.length - 1].Num = num;
    }
    $.cookie(cookieProductID, JSON.stringify(jsonObj), cookieSet); //寫入coockie  JSON需要json2.js支持
  }
  return totalNum;
  //  alert($.cookie(cookieProductID));
}

//以下為cookie插件代碼
jQuery.cookie = function(name, value, options) {
  if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
      value = '';
      options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
      var date;
      if (typeof options.expires == 'number') {
        date = new Date();
        date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
      } else {
        date = options.expires;
      }
      expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    var path = options.path ? '; path=' + options.path : '';
    var domain = options.domain ? '; domain=' + options.domain : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  } else { // only name given, get cookie
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
      var cookies = document.cookie.split(';');
      for (var i = 0; i < cookies.length; i++) {
        var cookie = jQuery.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if (cookie.substring(0, name.length + 1) == (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }
};

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery的cookie操作技巧總結》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》

希望本文所述對大家jQuery程序設計有所幫助。



主站蜘蛛池模板: 六一儿童节小品剧本| 接吻摸胸视频| 我在皇宫当巨巨| 影子是我的好朋友仿句怎么写 | 永不瞑目演员表| 188549.神秘的电影| 头像女可爱卡通| 亚洲成a人片在线观看| 王琳琳个人资料及简历| 一闪一闪亮晶晶钢琴谱简谱| 暖春| 奥特曼格斗进化重生破解版| 赵汉善| 变形金刚1原版免费观看| 赖小子| 欢乐钓鱼大师兑换码| 空姐一级毛片| 孤岛惊魂| 69视频免费看| 妇检被男医生摸到喷水| 小淘气尼古拉| 永远的牧歌简谱| 马德哈万| 卖梦的两人| 亚洲狠狠爱| 脚心的视频vk| 繁城| 架子姐姐| 看黄在线看| 赫伯曼电影免费观看| 铃木纱理奈| 戚薇床戏| 热带夜的引诱| 工伤赔偿协议书| 狼和狗的时间| 越南一级毛片免费| 雪山飞狐主题曲简谱| 电影《白月光》在线观看免费| 人民日报评墨茶| 八年级上册英语课文| 吉泽明步 番号 |

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103