2010-07-12 69 views
1

嗨我有以下功能,我需要使其僅返回false時,其他兩個複選框之一被選中。需要幫助inArray

$.validator.addMethod(
"ReutersNA", 
function(value, element) { 
    var selectedCountry = $("#Country").val(); 
    var NorthAmerica = new Array("USA","CAN","MEX"); 
    if($.inArray(selectedCountry,NorthAmerica) > -1) { 
    return true; 
    } else return false; 
    }, "Cannot select Reuters News outside of North America." 
); 

I need to add if($("#IQBAS, #IQPRE").is(":checked") && the above function = return true  

回答

1

你的意思是這樣的嗎?

$.validator.addMethod( 
"ReutersNA", 
function(value, element) { 
    var selectedCountry = $("#Country").val(); 
    var NorthAmerica = new Array("USA","CAN","MEX"); 
    return ($("#IQBAS, #IQPRE").is(":checked") && $.inArray(selectedCountry,NorthAmerica) > -1); 
}, 
"Cannot select Reuters News outside of North America." 
); 
+0

@ RightSaidFred如果不選擇#IQBAS和or #IQPRE,這仍然會返回「Can not ...」。我需要它返回味精,只有當你檢查IQBAS和或IQPRE – 2010-07-12 02:10:13

0

讓我們試着成爲一個小的通用...

$.validator.addMethod( 
"ReutersNA", 
function(value, element, params) { 
    var selectedCountry = value; 
    var NorthAmerica = new Array("USA","CAN","MEX"); 
    return (params && $.inArray(selectedCountry,NorthAmerica) > -1); 
}, 
"Cannot select Reuters News outside of North America." 
); 

,那麼你可以使用它像

rules: { 
    Country : { 
     ReutersNA : $("#IQBAS, #IQPRE").is(":checked") 
    } 
} 
+0

我看到這是如何工作的,驗證對參數被檢查的規則....不幸的是,無論檢查什麼,它都會返回錯誤 – 2010-07-12 03:34:39

+0

...什麼錯誤?? – Reigel 2010-07-12 03:52:12

0

你的問題有點不清楚。我認爲你的意思是它應該返回false(顯示消息),如果它被選中並且該元素不在數組中。

$.validator.addMethod( 
"ReutersNA", 
function(value, element) { 
    var selectedCountry = $("#Country").val(); 
    var NorthAmerica = new Array("USA","CAN","MEX"); 
    // Valid (true) if neither checked, or element is found in array. 
    return !$("#IQBAS, #IQPRE").is(":checked") || $.inArray(selectedCountry,NorthAmerica) > -1); 
}, 
"Cannot select Reuters News outside of North America." 
); 
+0

我得到這個錯誤 失蹤;在陳述前 [打破這個錯誤] || $ .inArray(selectedCountry,NorthAmerica)> -1); \ n – 2010-07-12 03:33:53

+0

@Dirty,把這兩個條件放在同一行(我只是調整了答案)。 – 2010-07-12 03:56:49