【发布时间】:2015-06-02 03:23:24
【问题描述】:
我有一个按钮,单击该按钮应删除 cookie,调用从文本框中收集值并将其保存为新 cookie 值的函数。
我遇到的问题是单击按钮后,函数正在执行,就好像 cookie 中有一个值一样。
我已经记录了 cookie 的值并且它显示为空。为什么我的函数没有发现该值为空?
//when new player is pressed
$('#BtnPlayAsNew').button().click(function () {
$.cookie('name', null); //delete cookie
$('#Result').hide();
getUsername(); //call function
});
function getUsername() {
//get cookie
var cookie = $.cookie('name');
//check value of cookie
if (cookie == null || cookie == "") {
$('#controls').hide();
$('#NewUser').show();
$('#BtnUser').click(function () {
name=$('#tbUser').val();
$.cookie('name', name,{path:'/' });
$('#NewUser').hide();
$('#controls').show();
});
}else{
$('#NewUser').hide();
$('#controls').show();
}
}
【问题讨论】:
-
您使用的是过期版本的 jquery-cookie。访问github.com/js-cookie/js-cookie/releases,获取第一个版本并阅读您想要的特定版本/标签的文档。正如@Samurai 指出的那样,删除的
null参数已被弃用并在几个月前被删除。
标签: javascript jquery cookies jquery-cookie js-cookie