Javascript 如何测试变量是否不等于两个值中的任何一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6115801/
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
How do I test if a variable does not equal either of two values?
提问by daGUY
I want to write an if/else statement that tests if the value of a text input does NOT equal either one of two different values. Like this (excuse my pseudo-English code):
我想编写一个 if/else 语句来测试文本输入的值是否不等于两个不同值中的任何一个。像这样(原谅我的伪英文代码):
var test = $("#test").val(); if (test does not equal A or B){ do stuff; } else { do other stuff; }
How do I write the condition for the if statement on line 2?
如何为第 2 行的 if 语句编写条件?
回答by
Think of !
(negation operator) as "not", ||
(boolean-or operator) as "or" and &&
(boolean-and operator) as "and". See Operatorsand Operator Precedence.
将!
(否定运算符)视为“非”,||
(布尔或运算符)视为“或”,将&&
(布尔与运算符)视为“与”。请参阅运算符和运算符优先级。
Thus:
因此:
if(!(a || b)) {
// means neither a nor b
}
However, using De Morgan's Law, it could be written as:
然而,使用德摩根定律,它可以写成:
if(!a && !b) {
// is not a and is not b
}
a
and b
above can be any expression (such as test == 'B'
or whatever it needs to be).
a
and b
above 可以是任何表达式(例如test == 'B'
或任何需要的表达式)。
Once again, if test == 'A'
and test == 'B'
, are the expressions, note the expansion of the 1st form:
再一次,如果test == 'A'
和test == 'B'
, 是表达式,请注意第一种形式的扩展:
// if(!(a || b))
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')
回答by CESCO
ECMA2016 Shortest answer, specially good when checking againt multiple values:
ECMA2016 最短答案,在检查多个值时特别好:
if (!["A","B", ...].includes(test)) {}
回答by James Montagne
In general it would be something like this:
一般来说,它会是这样的:
if(test != "A" && test != "B")
You should probably read up on JavaScript logical operators.
您可能应该阅读 JavaScript 逻辑运算符。
回答by Z. Zlatev
I do that using jQuery
我使用 jQuery 做到这一点
if ( 0 > $.inArray( test, [a,b] ) ) { ... }
回答by AlbertVo
var test = $("#test").val();
if (test != 'A' && test != 'B'){
do stuff;
}
else {
do other stuff;
}
回答by sophistihip
You used the word "or" in your pseudo code, but based on your first sentence, I think you mean and. There was some confusion about this because that is not how people usually speak.
您在伪代码中使用了“或”一词,但根据您的第一句话,我认为您的意思是“和”。对此有一些困惑,因为这不是人们通常的说话方式。
You want:
你要:
var test = $("#test").val();
if (test !== 'A' && test !== 'B'){
do stuff;
}
else {
do other stuff;
}
回答by JohnnyBoyy
May I suggest trying to use in else if statement in your if/else statement. And if you don't want to run any code that not under any conditions you want you can just leave the else out at the end of the statement. else if can also be used for any number of diversion paths that need things to be a certain condition for each.
我可以建议尝试在 if/else 语句中使用 else if 语句。如果你不想运行任何不在你想要的任何条件下的代码,你可以在语句的末尾留下 else 。else if 也可用于任意数量的分流路径,这些路径需要每个路径都满足特定条件。
if(condition 1){
如果(条件 1){
} else if (condition 2) {
}否则如果(条件2){
}else {
}别的 {
}
}