Home >Web Front-end >JS Tutorial >jQuery uses cookies and json to simply implement the shopping cart function_jquery

jQuery uses cookies and json to simply implement the shopping cart function_jquery

WBOY
WBOYOriginal
2016-05-16 15:05:141881browse

The example in this article describes how jQuery uses cookies and json to simply implement a shopping cart. Share it with everyone for your reference, the details are as follows:

1. Generate a cookie to store the product ID String type

2. When adding the product ID, convert the cookie into an array and check whether it is included. It does not include using js push to regenerate a new array.

3. Convert the array into String and store it in cookies.

The following is how jquery operates cookies and JSON

$.cookie("cart","ids",{expires:-7,path:"/"}) //存储cookie 保存7天
if($.cookie("cart")==undefined){//不存在用undefined}
var cartArr=new Array();
$.parseJSON($.cookie("cart"));//转化成数组
cartArr.push(id);//新的数组
JSON.stringify(cartArr);//这个好像是浏览器自带的 数组转化成json

Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery cookie operation skills summary", "jQuery table (table) operation skills summary" , "Summary of jQuery drag effects and techniques", "Summary of jQuery extension techniques", "Summary of jQuery common classic special effects", "jQuery animation and special effects usage summary", "jquery selector usage summary" and "jQuery common plug-ins and usage summary"

I hope this article will be helpful to everyone in jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn