您在这里有一个错字(selectedFeaturess),这可能与它有关:
if (jQuery.inArray(selectedFeaturess[i], elem.features) !== -1 || jQuery.inArray(selectedFeatures[i], elem.features) > -1) {
我怀疑这也无济于事:
<option value="Wendy's">Harveys</option>
像这样的代码:
if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1 || jQuery.inArray(selectedFeatures[i], elem.features) > -1) {
可以简化为:
if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) {
因为如果它 > -1 那么它就是 !== -1,所以第二个子句是多余的。例如,如果它 = 0,则触发 IF 子句的第一部分,无需执行 IF 语句的第二部分。
最后,这是您的 $(document).ready() 函数的重写。主要问题是您如何循环数组中的元素。无需将它们视为 jquery 选择器并对它们执行 .each() ,您只需执行一个简单的 For 循环。但是您可以使用 jquery 选择器来检查它们的长度。这对我有用(我还在选项中将 Wendy's 重命名为 Harveys)。
$(document).ready(function() {
//$('#filter').on('click', function(e) {
$('#filter').click(function(e) {
// prevent refreshing of the page
e.preventDefault();
// close all infoWindows
infowindow.close();
// hide all markers
$(markers.houses).each(function(index, elem) {
markers.houseMarkers[index].setVisible(false);
});
$(markers.condos).each(function(index, elem) {
markers.condoMarkers[index].setVisible(false);
});
// get the selected features from select box
var selectedFeatures = $("#features").val();
var selectedNearby = $("#nearby").val();
// for each house element...
$(markers.houses).each(function(index, elem) {
//first filter by selected features
if ($(selectedFeatures).length) {
for (var i = 0; i < selectedFeatures.length; i++) {
if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) {
if (!markers.houseMarkers[index].visible) {
markers.houseMarkers[index].setVisible(true);
}
}
}
}
//then filter by nearby selections
if ($(selectedNearby).length) {
for (var i = 0; i < selectedNearby.length; i++) {
if (jQuery.inArray(selectedNearby[i], elem.nearby) !== -1) {
// if marker is not visible, but meets the filtering criteria - show it
// otherwise leave it as it is
if (!markers.houseMarkers[index].visible) {
markers.houseMarkers[index].setVisible(true);
}
}
}
}
});
// for each condo element...
$(markers.condos).each(function(index, elem) {
// first filter by selected features
if ($(selectedFeatures).length) {
for (var i = 0; i < selectedFeatures.length; i++) {
if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) {
// if marker is not visible, but meets the filtering criteria - show it
// otherwise leave it as it is
if (!markers.condoMarkers[index].visible) {
markers.condoMarkers[index].setVisible(true);
}
}
}
}
//then filter by nearby selections
if ($(selectedNearby).length) {
for (var i = 0; i < selectedNearby.length; i++) {
if (jQuery.inArray(selectedNearby[i], elem.nearby) !== -1) {
// if marker is not visible, but meets the filtering criteria - show it
// otherwise leave it as it is
if (!markers.condoMarkers[index].visible) {
markers.condoMarkers[index].setVisible(true);
}
}
}
}
});
});
});