Removing a value from a cookie with Jquery and cookie.split
I have created the following script which loops over my cookie. When I find a match I want to remove just the matching value from the cookie. How do I achieve this?
var cookieName = 'myCookie';
var cookie = $.cookie("prefe开发者_运维技巧rences");
var cookie = cookie.split('|');
$(cookie).each(function(index){
var thisCookieData = this.split(',');
if(thisCookieData[0] == thisWidget.id ){
alert("Match!");
}
alert(thisCookieData);
});
You can use this code:
cookie = $.grep(cookie, function(item, index) {
var parts = item.split(',');
return parts[0] !== id;
}).join('|');
grep is a jquery function which finds elements in an array which satisfies the given function.
split, remove and join again:
.split(',')
...
.join(',')
精彩评论