检查多个cookies jQuery语法
我想检查两个cookie中是否设置了一个:检查多个cookies jQuery语法,jquery,cookies,syntax,Jquery,Cookies,Syntax,我想检查两个cookie中是否设置了一个: if($.cookie("c1") != 'true' || $.cookie("c2") != 'true') { (然后根据cookie执行一些操作或设置cookie) 这是检查是否设置了第一个或第二个的正确语法吗 谢谢。正确的语法是 if($.cookie("c1") !== null || $.cookie("c2") !== null) { // one of the cookies, or both, are set } el
if($.cookie("c1") != 'true' || $.cookie("c2") != 'true') {
(然后根据cookie执行一些操作或设置cookie)
这是检查是否设置了第一个或第二个的正确语法吗
谢谢。正确的语法是
if($.cookie("c1") !== null || $.cookie("c2") !== null) {
// one of the cookies, or both, are set
}
else {
// none of the cookies are set
}
根据您的评论,您所追求的可能是:
if($.cookie("c1") !== null && $.cookie("c2") !== null) {
// both cookies exist, let's check if they have the values we need
if($.cookie("c1") === "true" && $.cookie("c2") === "true") {
// they have 'true' as their content
}
else {
// they might exist, but both do not hold the value 'true'
}
}
else {
// none, or at least one of the two cookies are not set
}
谢谢,事实上,cookies的价值是“true”,但是看到你的评论“其中一个或两个都设置好了”帮助我理解了我的if语句不会以这种方式工作。。。我需要根据用户选择设置/检查具有两个不同值的cookie,而不是设置/检查两个cookie。要检查两者是否都设置了,您应该使用
和运算符,在javascript中,它是两个amp符号&&
,只有当两个cookie都存在时才会跳转到语句中。我已经更新了我的答案。谢谢,我试过了,它可能有效,但在我的情况下不行,你介意在另一个问题中看一下我的代码吗