我是否以正确的方式为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.")
}
帮助!
答案 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)
语法正确。
然而: