这是可能的编码
问题描述:
在jQuery
我想比较if()
括号中的多个值,我的要求如下。 当前我在jQuery
中试过这个,请告诉我它是否适用于其他环境。这是可能的编码
if((a==b && c==d) || (a!=b && c!=d)){
// code goes here
}
编辑
这是我的方案;
if (($(".product_attribute_input" + id + "_Color").children(":first").val() != "" && $(".product_attribute_input" + id + "_Size").children(":first").val() != "") || ($(".product_attribute_input" + id + "_Color").children(":first").val() !== undefined && $(".product_attribute_input" + id + "_Size").children(":first").val() !== undefined))
答
如果我理解您的方案正确,您要检查产品的颜色和大小的输入具有价值..
//Declare variable here for ease of maintenance and to avoid repetitive calls on the same node...
var productAttrInputColor = $(".product_attribute_input" + id + "_Color").children(":first").val(),
productAttrInputSize = $(".product_attribute_input" + id + "_Size").children(":first").val();
if (productAttrInputColor || productAttrInputSize) {
//Do something if the above condition returns a truthy values
}
此外,我除去条件运算符检查empty string
通知, undefined
。由于上述条件语句会将该值转换为布尔真,并执行代码块。
+0
完美的工作。谢谢 – user6594294
答
你想要一个反转异或操作。这可以缩短为:
if((a==b) == (c==d)){
// code goes here
}
那么究竟是什么问题? – Mureinik
还有什么其他的环境? – Vaibhav
@Mureinik以上条件是否有效?因为它不在我的代码 – user6594294