您在這裏有一個錯字(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