[英]jQuery.inArray(), returns -1 even if it contains the value in the array
The inarray function returns -1 even if it has the value in my array for eg. 即使inarray函数在例如我的数组中具有值,它也会返回-1。 if I have ['3','7','43','44','45','46']
in my array, when I select a value 3 or 7 it shows correct answer and then when I select other values it returns -1. 如果我在数组中有['3','7','43','44','45','46']
,则当我选择一个值3或7时,它将显示正确的答案,然后当我选择其他值时值返回-1。
my jQuery coding: 我的jQuery编码:
$(document).on("change", '#level',function() {
var valu = $('#level').val();
$.each(filterResults, function(index, item) {
if(jQuery.inArray(valu , item.id)!==-1){ //item.id has all array values
//my functions
}
}
});
then my HTML coding 然后我的HTML编码
<select class="tb2" name="level" id="level">
<option value="">Select Level</option>
<option value="3">Pre Test</option>
<option value="7">Level 1</option>
<option value="43">Level 2</option>
<option value="44">Level 3</option>
<option value="45">Level 4</option>
<option value="46">Level 5</option>
</select>
console.log(valu,item.id) returns console.log(value,item.id)返回
44 3 44 3
44 7 44 7
44 43 44 43
44 44 44 44
44 45 44 45
44 46 44 46
Try 尝试
if(jQuery.inArray(valu , filterResults)!==-1)
without .each()
没有.each()
var filterResults = ['3','7','43','44','45','46']; $(document).on("change", '#level',function() { var valu = $('#level').val(); // $.each(filterResults, function(index, item) { if(jQuery.inArray(valu , filterResults)!==-1){ //my functions console.log(valu, $.inArray(valu, filterResults)) } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <select class="tb2" name="level" id="level"> <option value="">Select Level</option> <option value="3">Pre Test</option> <option value="7">Level 1</option> <option value="43">Level 2</option> <option value="44">Level 3</option> <option value="45">Level 4</option> <option value="46">Level 5</option> </select>
item.id
does not contain an array, but a single id
. item.id
不包含数组,而是单个id
。 $.inArray
only works with arrays. $.inArray
仅适用于数组。 Try this instead: 尝试以下方法:
$(document).on("change", '#level',function() {
var valu = $('#level').val();
$.each(filterResults, function(index, item) {
if(valu == item.id){ // if item.id is equal to valu (e.g. '7' == '7')
// some code
}
});
});
Alternatively, you could filter the results using $.grep() to find out if the id is in there: 或者,您可以使用$ .grep()过滤结果以找出ID是否在其中:
$(document).on("change", '#level',function() {
var valu = $('#level').val();
if( $.grep(filterResults, function(){return this.id == valu}).length ){
// some code
}
});
ok, the problem in your code is that you are iterating through the results array for no reason. 好的,您代码中的问题是您无缘无故地遍历了结果数组。 $.inArray, will already iterate though the array without you having to do the work; $ .inArray,已经可以遍历数组,而无需您做工作; if the value exists, it will return the index of the item, if not, it will return -1. 如果该值存在,则将返回该项目的索引,如果不存在,则将返回-1。 With that being said, this is what you should do: 话虽如此,这是您应该做的:
var filterResults = ['3', '7', '43', '44', '45', '46'];
$('#level').change(function() {
var value = $(this).val();
if ($.inArray(value, filterResults) > -1) {
console.log(value + " EXISTS IN ARRAY");
} else {
console.log("FALSE");
}
});
Here's a FIDDLE for further help. 这是一个需要进一步帮助的字段 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.