在JavaScript中的Cookie阵列 [英] Cookie Array in javascript

查看:120
本文介绍了在JavaScript中的Cookie阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米试图建立一个二维数组在JavaScript中的cookie。这可能吗?首先,如何创建这个数组饼干吗?所有的二,如何在这个数组中的Cookie为了做我循环很多检索数据。谢谢!

I"m trying to create a 2 dimensional array as a cookie in javascript. Is this possible? First of all, how do I create this array cookie? Second of all, how do I loop through this array cookie in order to retrieve data. Thanks alot!!!

推荐答案

您结合使用 jQuery.cookie 插件, JSON 并解决您的问题。

You combine the use jQuery.cookie plugin and JSON and solve your problem.

当你想存储阵列,您可以在JS创建一个数组,并使用 JSON.stringify 将其改造成一个字符串,并存储在 $ .cookie('名','array_string')

When you want to store an array,You created an array in JS and use JSON.stringify to transform it into an string and stored with $.cookie('name', 'array_string')

var myAry = [1, 2, 3];
$.cookie('name', JSON.stringify(myAry));

当你想以检索数组中的cookie里面的,可以使用 $。cookie的('名称')以检索cookie的值,并使用 JSON.parse 以检索从字符串数组。

When you want to retrive the array inside the cookie, you use $.cookie('name') to retrive the cookie value and use JSON.parse to retrive the array from the string.

var storedAry = JSON.parse($.cookie('name'));
//storedAry -> [1, 2, 3]

这篇关于在JavaScript中的Cookie阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆