【发布时间】:2016-08-08 20:24:11
【问题描述】:
我构建了一个很好的工具,以便在谷歌地图上找到我的商店,该地图从 javascript 对象中迭代每个位置。当我的 .search 函数找不到任何结果时,我需要一个“无搜索结果”。问题是我的代码无论如何都会对每个位置进行迭代,因此它将为我的所有位置创建多个文本。有没有办法解决这个问题?这是我的代码:
javascript:
var output = '<div class="searchresults">';
$.each(markers, function(key, markers) {
output += '<div>';
output += '<h2>'+ markers.title +'</h2>';
output += '<div>'+ markers.html +'</div>';
output += '<div>'+ markers.city +'</div>';
output += '<div>'+ markers.postalcode +'</div>';
output += '<div>'+ markers.phone +'</div>';
output += '</div>';
});
output += '</div>';
$('#legend').html(output);
$('#geocomplete').keyup(function() {
var searchField = $('#geocomplete').val();
var myExp = new RegExp(searchField, "i");
var output = '<div class="searchresults">';
$.each(markers, function(key, markers) {
if (markers.city.search(myExp) == -1 && markers.postalcode.search(myExp) == -1) {
output += ( "<p>No search result</p>" );
} else {
output += '<div>';
output += '<h2>'+ markers.title +'</h2>';
output += '<div>'+ markers.html +'</div>';
output += '<div>'+ markers.city +'</div>';
output += '<div>'+ markers.postalcode +'</div>';
output += '<div>'+ markers.phone +'</div>';
output += '</div>';
}
});
output += '</div>';
$('#legend').html(output);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
和html:
<div class="container-fluid map">
<h2>Where To Buy</h2>
<fieldset>
<legend>Find a Store Near You</legend>
<form>
<input id="geocomplete" class="controls" type="text" placeholder="Start typing your City or Postal Code to get your Chickapea!" size="60" />
<input id="find" type="button" value="Find" />
</form>
</fieldset>
<div id="legend"></div>
<div class="container-fluid" id="map-canvas-two"></div>
</div>
</body>
</html>
【问题讨论】:
-
对,所以...它现在在做什么,而您希望它做什么?
-
我在帖子里解释过,if (markers.city.search(myExp) == -1 && markers.postalcode.search(myExp) == -1) { output += ( "
没有搜索结果
" ); } 不会输出我所有位置的搜索结果,但我需要它只输出一个结果 -
我问是因为不清楚。你能用另一种方式解释吗?
-
我贴出了整个代码
-
我不知道这个问题很难理解。
标签: jquery google-maps search iteration javascript-objects