需要有关 jQuery $.inArray() 的帮助
谁能告诉我这段代码有什么问题吗?
jQuery(document).ready(function(){
var val = ["Hourly", "Daily", "Weekly", "Monthly", "Yearly"];
var myArr = ["Weekly", "something"];
$( myArr ).each(function( j ){
if ( $.inArray( myArr[j] == val ) ) {
alert( 'yes, Matched !!' );
console.log( myArr[j] );
} else {
alert( 'Nops ' );
}
});
//console.log( val );
});
我需要匹配数组元素,我使用了 $.inArray()
,但它永远不会进入 ELSE 条件,即使它不存在于数组中。 任何帮助将不胜感激。
Could anybody tell me what is wrong with this code ??
jQuery(document).ready(function(){
var val = ["Hourly", "Daily", "Weekly", "Monthly", "Yearly"];
var myArr = ["Weekly", "something"];
$( myArr ).each(function( j ){
if ( $.inArray( myArr[j] == val ) ) {
alert( 'yes, Matched !!' );
console.log( myArr[j] );
} else {
alert( 'Nops ' );
}
});
//console.log( val );
});
I need to match the array elements, i used $.inArray()
, but it never goes to ELSE condition even it doesn't exist in the array.
Any help would be appreciatd.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$.inArray()
采用两个参数,即值和数组,并返回> -1
如果找到匹配项,那么它应该是这样的:你可以在这里测试它。另请注意
$.each()
用于非元素集,没有理由创建无效的 jQuery 对象来运行循环。$.inArray()
takes two arguments, the value and the array, and returns> -1
if it finds a match, so it should be like this:You can test it here. Also note the use of
$.each()
for non-element sets, no reason creating an invalid jQuery object to run the loop.$.inArray
当在数组中找不到值时返回-1
,否则返回数组中的位置,可能是0
代码>.$.inArray
returns-1
when it doesn't find the value in the array, otherwise it returns the position in the array, which could be0
.