开发者

Need help with inArray

Hi I have the following function, I need to make it return false only if one of two other checkboxes are checked.

$.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 ou开发者_StackOverflow中文版tside of North America."
);

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


You mean something like this?

$.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."   
);  


let us try to be a little generic...

$.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."   
);

then you could use it like

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


Your question was a little unclear. I think you mean it should return false (show the message) if it's checked and the element is not in the array.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜