【问题标题】:How to get cookie subkeys with jquery cookie plugin?如何使用 jquery cookie 插件获取 cookie 子键?
【发布时间】:2012-07-01 21:57:52
【问题描述】:

在我的 .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 asp.net jquery-cookie


    【解决方案1】:

    如果 $.cookie("poll") 是一个数组,这将起作用:

    var mycookie = $.cookie("poll");
    if(mycookie){
       var poll_id=mycookie["poll_id"];
    }
    

    您可以使用 firebug 或 chrome 开发工具检查 mycookie 变量的类型。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 2014-01-25
        相关资源
        最近更新 更多