需要帮助inArray

时间:2010-07-12 01:53:10

标签: javascript jquery arrays

您好我有以下功能,只有在选中其他两个复选框之一时才需要返回false。

$.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    

3 个答案:

答案 0 :(得分: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."   
);  

答案 1 :(得分: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")
     }
 }

答案 2 :(得分: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."   
);