gpt4 book ai didi

jquery - 如何使用 jquery cookie 插件获取 cookie 子键?

转载 作者:行者123 更新时间:2023-12-01 03:19:45 27 4
gpt4 key购买 nike

在我的 .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???
}

任何想法将不胜感激。

最佳答案

尝试添加这个插件 - 它要求 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

关于jquery - 如何使用 jquery cookie 插件获取 cookie 子键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com