在我的.aspx页面中,当有人按照以下方式投票时我设置了一个cookie;
HttpContext.Current.Response.Cookies("poll")("poll_voted") = "yes"
HttpContext.Current.Response.Cookies("poll")("poll_id") = pID
HttpContext.Current.Response.Cookies("poll").Expires = Date.Now.AddDays(30)
现在,jquery cookie plugin我需要检查cookie是否存在;
// this works quite well...
if ($.cookie('poll', { poll_voted: 'yes' })) {
// now here, I need to get the value of poll_id but how???
}
任何想法都会受到赞赏。
答案 0 :(得分:3)
尝试添加此插件 - 它要求jquery.cookies.js已经在页面上,我确信它可以使用一些调整,但它对我正在做的事情已经足够了。
(function ($, document) {
if($.cookie){
$.cookieKey = function(CookieName, KeyName, Value, Options){
var reg = new RegExp("(?:([^=]+)=([^&]*)&?)", "ig"),
match = null,
matches = [];
var cookieVal = $.cookie(CookieName);
while(match = reg.exec(cookieVal)){
if(KeyName.toLowerCase() == match[1].toLowerCase()){
if(Value){ //we are updating, collect all values
matches.push([match[1], Value]);
}
else{
return match[2]; //we are getting, sub key found just return it
}
}
else if(Value){ //we are updating, collect all values
matches.push([match[1], match[2]]);
}
}
if(Value){ //we are updating, update values
updatedValue = "",
sep = "";
for(i=0;i<matches;i++){
updatedValue += sep + matches[i][0] + "=" + matches[i][1];
sep = "&"
}
$.cookie(CookieName, updatedValue, Options);
}
else return null;//we are getting, value not found
}
}
})(jQuery, document);
$.cookieKey("mycookiename", "keyname");//get sub key value
$.cookieKey("mycookiename", "keyname", "value");//set sub key value
$.cookieKey("mycookiename", "keyname", "value", {path: "/"});//set sub key value with cookie options
答案 1 :(得分:1)
如果$ .cookie(&#34; poll&#34;)是一个数组,这将有效:
var mycookie = $.cookie("poll");
if(mycookie){
var poll_id=mycookie["poll_id"];
}
您可以使用firebug或chrome开发人员工具检查mycookie
变量的类型。