欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery基于json與cookie實(shí)現(xiàn)購物車的方法

 更新時(shí)間:2016年04月15日 10:19:06   作者:jarryzhong  
這篇文章主要介紹了jQuery基于json與cookie實(shí)現(xiàn)購物車的方法,涉及jQuery操作json格式數(shù)據(jù)與cookie存儲(chǔ)購物車信息的相關(guān)技巧,需要的朋友可以參考下

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

json 格式:

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

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

/*
添加商品及數(shù)量到購物車cookie中,返回當(dāng)前商品在cookie中的總數(shù)
*/
function AddToShoppingCar(id, num) {
  var _num = 1;
  if (num != undefined)
    _num = num;
  var totalNum = _num; //總數(shù)默認(rèn)為傳入?yún)?shù)
  var cookieSet = { expires: 7, path: '/' }; //設(shè)置cookie路徑的
//  $.cookie(cookieProductID, null, cookieSet);//清除Cookie
  var jsonStr = "[{'ProductID':'" + id + "','Num':'" + _num + "'}]"; //構(gòu)造json字符串,id是商品id  num是這個(gè)商品的數(shù)量
  if ($.cookie(cookieProductID) == null) {
    $.cookie(cookieProductID, jsonStr, cookieSet); //如果沒有這個(gè)cookie就設(shè)置他
  }
  else {
    var jsonObj = eval('(' + $.cookie(cookieProductID) + ')'); //如果有,把json字符串轉(zhuǎn)換成對象
    var findProduct = false;//是否找到產(chǎn)品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;
  }
};

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

希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 鼠標(biāo)移到導(dǎo)航當(dāng)前位置的LI變色處于選中狀態(tài)

    鼠標(biāo)移到導(dǎo)航當(dāng)前位置的LI變色處于選中狀態(tài)

    本文所要實(shí)現(xiàn)的效果廣泛應(yīng)用于當(dāng)前導(dǎo)航,主要表現(xiàn)為鼠標(biāo)移到導(dǎo)航上面當(dāng)前的LI變色,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下,希望對大家有所幫助
    2013-08-08
  • jQuery Collapse1.1.0折疊插件簡單使用

    jQuery Collapse1.1.0折疊插件簡單使用

    這篇文章主要介紹了jQuery Collapse1.1.0折疊插件的使用方法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • jquery獲取復(fù)選框被選中的值

    jquery獲取復(fù)選框被選中的值

    這篇文章主要介紹了如何使用jquery獲取復(fù)選框被選中的值,需要的朋友可以參考下
    2014-04-04
  • jQuery中prev()方法用法實(shí)例

    jQuery中prev()方法用法實(shí)例

    這篇文章主要介紹了jQuery中prev()方法用法,實(shí)例分析了prev()方法的功能、定義及取得匹配元素集合中每個(gè)元素緊鄰的前一個(gè)同輩元素使用技巧,需要的朋友可以參考下
    2015-01-01
  • JQuery記住用戶名密碼實(shí)現(xiàn)下次自動(dòng)登錄功能

    JQuery記住用戶名密碼實(shí)現(xiàn)下次自動(dòng)登錄功能

    這篇文章主要介紹了JQuery記住用戶名密碼實(shí)現(xiàn)下次自動(dòng)登錄功能,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04
  • 解析Jquery的LigerUI如何實(shí)現(xiàn)文件上傳

    解析Jquery的LigerUI如何實(shí)現(xiàn)文件上傳

    本篇文章是對Jquery中的LigerUI實(shí)現(xiàn)文件上傳的方法,進(jìn)行了分析介紹,需要的朋友可以參考下
    2013-07-07
  • 最新評論