在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:14:43  来源:igfitidea点击:

Using OR operator in a jquery if statement

jqueryif-statementoperators

提问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 stateequals 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 statewill 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 trueexactly when xis true and falseexactly when xis false. You can just do:

没有必要。在这种情况下,该方法true准确地返回whenx为 true 和falsewhen xis false。你可以这样做:

return x;

回答by Krishna

The code you wrote will always return truebecause statecannot be both 10 and 15 for the statement to be false. if ((state != 10) && (state != 15)....ANDis 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代替。这将返回数组中元素的索引。

JSFIDDLE DEMO

JSFIDDLE 演示

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 statvalue is one of arrelements

更新:使用.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,在第一个条件处停止。