开发者

Jquery: Retrieve array from 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜