IF语句的多个OR条件

时间:2013-12-17 15:37:20

标签: javascript if-statement

我是否以正确的方式为IF语句执行多个OR条件?

var A0minWidth = 841;
var A0minHeight = 1189;

var A0minWidthBleed = 847;
var A0minHeightBleed = 1195;

UploadedDocNameHeightMM = //(get it from the database)
UploadedDocNameWidthMM = //(get it from the database)

if(UploadedDocNameHeightMM < parseFloat(A0minHeight) || UploadedDocNameWidthMM < parseFloat(A0minWidth) || UploadedDocNameWidthMM > parseFloat(A0minWidthBleed) || UploadedDocNameHeightMM > parseFloat(A0minHeightBleed)) 
{
       //do this
       alert ("Yes! one of those.")
}

帮助!

2 个答案:

答案 0 :(得分:3)

这取决于你的代码当然应该做什么,但从语法上来说这是正确的 - 例如不需要将每个作为操作数的表达式包装到括号中的逻辑或运算符,如下所示:

if ((UploadedDocNameHeightMM < parseFloat(A0minHeight)) || (UploadedDocNameWidthMM < parseFloat(A0minWidth)) || (UploadedDocNameWidthMM > parseFloat(A0minWidthBleed)) || (UploadedDocNameHeightMM > parseFloat(A0minHeightBleed)))
{
    alert("Yes! one of those.");
}

此外,||运算符将进行短路评估。基本上它不会评估任何计算结果为true的表达式右侧的表达式。

有关||和其他JavaScript逻辑运算符(包括示例)的详细信息,请查看Mozilla's overview或搜索JavaScript逻辑运算符。

答案 1 :(得分:0)

语法正确。

然而:

  • 此处不需要parseFloat
  • 您确定两个首次测试是否正确(==&gt;您是否需要检查UploadedDocNameHeightMM&gt; A0minHeight而不是&lt;?)