【发布时间】:2014-01-20 19:24:16
【问题描述】:
我在网上找不到答案,所以这里是:
我正在从查询字符串中读取一个字符串,并使用 jquery cookie 插件将其保存在一个 cookie 中。
这个页面可能每天用不同的 id 重新加载几次,但它似乎总是记住第一个值。每次让cookie都变成新的值似乎很困难。
$.cookie('SomeId') // already contains some number from the first time the page was called.
我试图使用的逻辑是这样的:
if (QueryString.some_id) {
alert(QueryString.some_id); //shows new Id
$.cookie('SomeId', null, { expires: -1 }); //Tried deleting old cookie first
$.cookie('SomeId', QueryString.some_id, { expires: 30, path: '/' }); // Also tried without the path param
alert($.cookie('SomeId')); // Alerts the OLD id. Previous call didn't work.
}
如何让$.cookie('SomeId') 可更新?
如果它很重要,我怀疑它可能,我通过使用 IIS 在本地运行项目来运行这些测试。这是一个 MVC 应用程序。
谢谢
【问题讨论】:
-
为我工作,jsfiddle.net/rooseve/5Tp9n。确保始终设置 { path: '/' }。
-
如果你第一次尝试像
$.removeCookie('SomeId');这样删除cookie会怎样? -
它适用于 jFiddle,但在我的项目中,每次我刷新页面时,$.cookie('SomeId') 都会卡在第一个值上。是否可能因为 URL 是“localhost”而失败?
-
我试过 $.removeCookie('SomeId');仍然没有运气
标签: jquery jquery-cookie