Javascript jQuery无法运行
在我的验证中,必填字段可以更改。我有一个需要的输入ID数组(必需)。如果数组包含字符串“All”,则需要所有输入Javascript jQuery无法运行,javascript,jquery,Javascript,Jquery,在我的验证中,必填字段可以更改。我有一个需要的输入ID数组(必需)。如果数组包含字符串“All”,则需要所有输入 function getIfRequired(id){ console.log("id: "+id); console.log(required); console.log("inarray: "+jQuery.inArray(required, 'All')); if (jQuery.inArray(required, 'All') > -1
function getIfRequired(id){
console.log("id: "+id);
console.log(required);
console.log("inarray: "+jQuery.inArray(required, 'All'));
if (jQuery.inArray(required, 'All') > -1){ return true; }
else if(jQuery.inArray(required, id) != -1){ return true; }
else{ return false; }
}
我正在尝试使用JQuery.inArray()来确定是否需要
function getIfRequired(id){
console.log("id: "+id);
console.log(required);
console.log("inarray: "+jQuery.inArray(required, 'All'));
if (jQuery.inArray(required, 'All') > -1){ return true; }
else if(jQuery.inArray(required, id) != -1){ return true; }
else{ return false; }
}
但它不断返回'-1'(未找到)
以下是日志输出示例:
id: clientname
["clientname", "sourceid", "clientcountry","clienttown", "clienttownid", "typeoflaw"]
inarray: -1
id: clientname
["All"]
inarray: -1
为什么它不工作?您的值和数组参数被转换,例如:
jQuery.inArray('All', required);
因为在
All
你将写如下
console.log("inarray: "+jQuery.inArray('All', required));
你打错了
jQuery.inArray
。首先是值,然后是数组
看
写
jQuery.inArray('All', required);
而不是
jQuery.inArray(required, 'All');
为什么您发送的是
'All'
而不是id
?此外,第一个参数应该是值,第二个参数应该是数组。如果您愿意阅读,您会注意到方法签名是inArray(value,array)
;)facepalm-总有一天,人们会在编写自己的语法之前查看函数文档,并感到困惑。facepalm的确如此!!!Tbf我从现有代码中获得jquery.inarray行,以前从未使用过它。教我假设它是correct@SmokeyPHP我非常怀疑这是否会成为一种趋势