2016-07-27 36 views
0

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)) 
+1

那麼究竟是什麼問題? – Mureinik

+0

還有什麼其他的環境? – Vaibhav

+0

@Mureinik以上條件是否有效?因爲它不在我的代碼 – user6594294

回答

0

如果我理解您的方案正確,您要檢查產品的顏色和大小的輸入具有價值..

//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

3

你想要一個反轉異或操作。這可以縮短爲:

if((a==b) == (c==d)){ 
// code goes here 

}