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