简体   繁体   English

需要帮助inArray

[英]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. 您好,我具有以下功能,只有在选中其他两个复选框之一时,才需要使它返回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    

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. 我认为您的意思是,如果选中它并且该元素不在数组中,则它应该返回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."   
);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM