【问题标题】:Stop jquery iteration if the .search method will be equal to -1如果 .search 方法等于 -1,则停止 jquery 迭代
【发布时间】: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


【解决方案1】:

我了解您的问题,即最小、完整和可验证。
;)

见我的CodePen here

您只需要一个“标志”来确定是否未找到任何结果...
为了在循环迭代中阻止这种“重复未找到结果提及”。

AND 将此“未找到结果”置于each() 循环之外。

这是修改后的代码部分:

$('#geocomplete').keyup(function() {
    var searchField = $('#geocomplete').val();
    var myExp = new RegExp(searchField, "i");
    var output = '<div class="searchresults">';

    var flagUnfound = true;
    $.each(markers, function(key, markers) {


        if (markers.city.search(myExp) != -1 || markers.postalcode.search(myExp) != -1) {
          flagUnfound = false;

            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>';

        }
    });

    if(flagUnfound){
      output += ( '<p>No search result</p>' );
    }
    output += '</div>';
    $('#legend').html(output);
});

尝试在输入中使用“L9Y”作为邮政编码。

【讨论】:

  • 它就像一个魅力!谢谢你,我一直在努力!
  • 再次编辑以获得完美的“不可批评”的答案。 ;)
猜你喜欢
  • 2014-02-02
  • 2017-12-10
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2014-12-01
  • 2013-11-01
相关资源
最近更新 更多