使用 AND if 语句的多个条件,javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47032704/
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-10-29 07:08:07  来源:igfitidea点击:

Multiple conditions using an AND if Statement, javascript

javascriptif-statementmultiple-conditions

提问by TheWee Seal

Can anyone tell me what's wrong with this ifstatement?

谁能告诉我这个if语句有什么问题?

If I use either of the two main conditions on their own the statement works fine but when I add that middle && statement, it stops working. I've searched online and can't see what's wrong.

如果我自己使用两个主要条件中的任何一个,则该语句可以正常工作,但是当我添加中间&& 语句时,它会停止工作。我在网上搜索过,看不出有什么问题。

P.S. I can even change that middle &&to a ||statement and it works as well. I'm so confused.

附注。我什至可以将中间的&&更改为|| 声明,它也有效。我很混乱。

if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") && (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
        {
            alert("finished");
            cdpause();      
        }

回答by gurvinder372

I've searched online and can't see what's wrong.

I can even change that middle && to a || statement and it works as well

我在网上搜索过,看不出有什么问题。

我什至可以将中间的 && 更改为 || 声明,它也有效

Because containerId.idcan't be LineOneand LineTwoat the same time.

因为containerId.id不能LineOneLineTwo在同一时间。

Similarly, dropLoccan't have two values at the same time.

同样,dropLoc不能同时拥有两个值。

But it can have one of the two values, so replace &&with ||.

但它可以具有两个值之一,因此替换&&||.

   if ((containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || 
       (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"))
   {
        alert("finished");
        cdpause();      
   }

回答by Patrick Hund

You already had the correct solution, you should have a ||(or) instead of a &&(and) in the middle.

你已经有了正确的解决方案,你应该有一个|| (or) 而不是中间的&&(and)。

It's basic boolean logic: you have two expressions with "and", and you want to execute your code if either of those expressions are true, so you join those expressions with "or".

这是基本的布尔逻辑:你有两个带有“and”的表达式,如果其中一个表达式为真,你想执行你的代码,所以你用“or”连接这些表达式。

"If the name is John and it's Monday OR if the name is Jane and it's Tuesday , then remind them to shop for groceries." => on Monday, it's John's turn to go shopping, on Tuesday, it is Jane's.

“如果名字是约翰,现在是星期一,或者如果名字是简,现在是星期二,那么提醒他们去买杂货。” => 星期一轮到约翰去购物,星期二轮到简去购物。

回答by Nina Scholz

You could combinte the two checks with an OR, because, you can not have two different values at the same time.

您可以将两个检查与 OR 组合在一起,因为您不能同时拥有两个不同的值。

Beside that, you need no brackets, because of the operator precedenceof logical AND &&over logical OR ||.

除了这一点,你不需要支架,因为的运算符优先级逻辑与&&逻辑OR||

if (
    containerId.id == "LineOne" && dropLoc == "dropLocation1.1" ||
    containerId.id == "LineTwo" && dropLoc == "dropLocation2.2"
) {
    alert("finished");
    cdpause();      
}

回答by vSR3P

Like Satpal said

就像 Satpal 说的

 if(containerId.id == "LineOne" && dropLoc == "dropLocation1.1") || (containerId.id == "LineTwo" && dropLoc == "dropLocation2.2")