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

新手上路,请多包涵

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

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

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

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

原文由 TheWee Seal 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 240
1 个回答

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

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

因为 containerId.id 不能同时是 LineOneLineTwo

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

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

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

原文由 gurvinder372 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题