在 jquery if 语句中使用 OR 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40221998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using OR operator in a jquery if statement
提问by Ledattack
I need to use the OR operator in a jQuery if statement to filter out 10 states. My code works wile only excluding one state, but fails when i try to include multiple states. Is there a correct way to do this?
我需要在 jQuery if 语句中使用 OR 运算符来过滤掉 10 个状态。我的代码只排除一种状态,但当我尝试包含多个状态时失败。有没有正确的方法来做到这一点?
Code I am am using :
我正在使用的代码:
if ((state != 10) || (state != 15) || (state != 19) ||
(state != 22) || (state != 33) || (state != 39) ||
(state != 47) || (state != 48) || (state != 49) ||
(state != 51))
return true;
Thanks
谢谢
回答by EJoshuaS - Reinstate Monica
Think about what
想什么
if ((state != 10) || (state != 15) || (state != 19) || (state != 22) || (state != 33) || (state != 39) || (state != 47) || (state != 48) || (state != 49) || (state != 51))
means. ||
means "or." The negation of this is (by DeMorgan's Laws):
方法。||
意思是“或”。对此的否定是(根据德摩根定律):
state == 10 && state == 15 && state == 19...
In other words, the only way that this could be false if if a state
equals 10, 15, and 19 (and the rest of the numbers in your or statement) at the same time, which is impossible.
换句话说,如果 a 同时state
等于 10、15 和 19(以及 or 语句中的其余数字),则这可能是错误的唯一方法,这是不可能的。
Thus, this statement will alwaysbe true. State 15 will never equal state 10, for example, so it's alwaystrue that state
will either not equal 10 or not equal 15.
因此,这种说法将永远是正确的。例如,状态 15 永远不会等于状态 10,因此,不等于 10 或不等于 15总是正确的state
。
Change ||
to &&
.
更改||
为&&
。
Also, in most languages, the following:
此外,在大多数语言中,以下内容:
if (x) {
return true;
}
else {
return false;
}
is not necessary. In this case, the method returns true
exactly when x
is true and false
exactly when x
is false
. You can just do:
没有必要。在这种情况下,该方法true
准确地返回whenx
为 true 和false
when x
is false
。你可以这样做:
return x;
回答by Krishna
The code you wrote will always return true
because state
cannot be both 10 and 15 for the statement to be false. if ((state != 10) && (state != 15)....
AND
is what you need not OR
.
您编写的代码将始终返回,true
因为state
该语句不能同时为 10 和 15。if ((state != 10) && (state != 15)....
AND
是你不需要的OR
。
Use $.inArrayinstead. This returns the index of the element in the array.
使用$.inArray代替。这将返回数组中元素的索引。
var statesArray = [10, 15, 19]; // list out all
var index = $.inArray(state, statesArray);
if(index == -1) {
console.log("Not there in array");
return true;
} else {
console.log("Found it");
return false;
}
回答by Mamdouh Saeed
Update:using .indexOf()
to detect if stat
value is one of arr
elements
更新:使用.indexOf()
以检测stat
值中的一个arr
元素
Pure JavaScript
纯 JavaScript
var arr = [20,30,40,50,60,70,80,90,100];
//or detect equal to all
//var arr = [10,10,10,10,10,10,10];
var stat = 10;
if(arr.indexOf(stat)==-1)alert("stat is not equal to one more elements of array");
回答by four
The logical OR '||' automatically short circuits if it meets a true condition once.
逻辑 OR '||' 一次满足真条件自动短路。
false || false || true|| false = true, stops at second condition.
假|| 假|| 真|| false = true,在第二个条件停止。
On the other hand, the logical AND '&&' automatically short circuits if it meets a false condition once.
另一方面,如果逻辑 AND '&&' 满足一次错误条件,它会自动短路。
false && true&& true && true = false, stops at first condition.
false && true&& true && true = false,在第一个条件处停止。