需要有关jQuery $ .inArray()的帮助 [英] Need help regarding jQuery $.inArray()
本文介绍了需要有关jQuery $ .inArray()的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我这段代码有什么问题吗?
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 );
});
我需要匹配数组元素,我使用了$.inArray()
,但是即使数组中不存在它也永远不会达到ELSE条件.
任何帮助将不胜感激.
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.
推荐答案
$.inArray()
接受两个参数,即值和数组,如果找到匹配项,则返回> -1
,因此应如下所示:
$.inArray()
takes two arguments, the value and the array, and returns > -1
if it finds a match, so it should be like this:
jQuery(document).ready(function(){
var val = ["Hourly", "Daily", "Weekly", "Monthly", "Yearly"];
var myArr = ["Weekly", "something"];
$.each(myArr, function(i, v) {
if ($.inArray(v, val) != -1) {
alert( 'yes, Matched !!' );
console.log(v);
} else {
alert( 'Nops ' );
}
});
});
您可以在此处进行测试.还要注意对非元素集使用 $.each()
,没有理由创建无效的jQuery对象来运行循环.
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.
这篇关于需要有关jQuery $ .inArray()的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文