开发者

Jquery: Retrieve array from cookie

开发者 https://www.devze.com 2022-12-22 12:48 出处:网络
im having some difficulty trying to pull out a specific value from a cookie. im using the cookie plugin found here: http://plugins.jquery.com/project/cookie

im having some difficulty trying to pull out a specific value from a cookie. im using the cookie plugin found here: http://plugins.jquery.com/project/cookie

var cookieString = "{'zip':'" + $( '#ZipCode' ).val() + "','age':'" + $( '#age' ).val() + "','gender':'" + $( '#gender' ).val() +"}";
$.cookie("rememberMe", ( ($( '#rememberMe' 开发者_开发技巧).attr( 'checked' ))?cookieString:null ), { path: '/', expires: 60 });
alert($.cookie("rememberMe"));

which will return correctly:

{'zip':'91210','age':'99','gender':'male'}

now im just having trouble with pulling out specifically one of the items in the array. for example how would i just pull out the value of 'zip'?


You can use eval to invoke the Javascript compiler and convert the string into an object:

var obj = eval('(' + $.cookie("rememberMe") + ')');
alert(obj['zip']);
0

精彩评论

暂无评论...
验证码 换一张
取 消