我想检查是否设置了两个 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
}
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
}