我尝试使用inarray,但它总是返回true?有什么想法吗?(所有的li都显示出来了)
$("#select-by-color-list li").hide();
// get the select
var $dd = $('#product-variants-option-0');
if ($dd.length > 0) { // make sure we found the select we were looking for
// save the selected value
var selectedVal = $dd.val();
// get the options and loop through them
var $options = $('option', $dd);
var arrVals = [];
$options.each(function(){
// push each option value and text into an array
arrVals.push({
val: $(this).val(),
text: $(this).text()
});
});
};
//This is where it is returning true...
if($.inArray('Aqua', arrVals)) {
$("#select-by-color-list li#aqua").show();
};
if($.inArray('Army', arrVals)) {
$("#select-by-color-list li#army").show();
};
发布于 2017-02-22 20:53:55
ES6出手相救!虽然不是jQuery,但我认为值得为未来的读者回答……
ES6引入了.includes()
,它的工作方式是你认为$.inArray
应该工作的方式:
const myArray = ["a", "b", "c"];
console.log(myArray.includes("a")) /* true */
console.log(myArray.includes("d")) /* false */
https://stackoverflow.com/questions/4558086
复制相似问题