简体   繁体   English

JavaScript if语句中的多个或语句

[英]Multiple or statements in a Javascript if statement

Okay, I'm fairly comfortable with basic If statements and know that if I want to say "When X is equal to 1 and Y is equal to 2 do whatever" I can write it as: 好的,我对基本的If语句相当满意,并且知道如果我想说“当X等于1且Y等于2时,请执行任何操作”,我可以这样写:

if ((X=="1") && (Y=="2")) { 

But how do you write this: 但是你怎么写这个:

If X is equal to 1 or 2 and Y is equal to 1 or 2, do whatever... 如果X等于1或2并且Y等于1或2,则执行任何操作...

I tried the following, but none of these work. 我尝试了以下方法,但是这些都不起作用。

if ((X=="1" || x=="2") && (Y=="2" || y=="1")) { 

if ((X=="1") || (x=="2") && (Y=="2") || (Y=="1")) { 

Can someone explain to me what the format is? 有人可以告诉我什么格式吗?

与大多数语言一样,JavaScript区分大小写。

if ((X == "1" || X == "2") && (Y == "2" || Y == "1")) { 

if ((X=="1" || x=="2") && (Y=="2" || y=="1")) {很好,只是Xx是不同的变量Yy )。

检查您的情况, xX不同。


Be careful with the names of your variables (they are case-sensitive): 请注意变量名称(它们区分大小写):

x is not the same as X xX不同
y is not the same as Y yY不同

this should work: 这应该工作:

if ((X=="1" || X=="2") && (Y=="1" || Y=="2")) { 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM