您在这里有一个错字(selectedFeaturess),这可能是与它过滤代码:
if (jQuery.inArray(selectedFeaturess[i], elem.features) !== -1 || jQuery.inArray(selectedFeatures[i], elem.features) > -1) {
此外,这不是t Ø帮助我怀疑:
<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);
}
}
}
}
});
});
});
我会建议广泛使用控制台。登录尝试并找出过滤执行的每个阶段发生了什么 – duncan
@duncan:真的吗?我自己不会这么想。 – Bob
等等记录的结果告诉你什么? – duncan