【问题标题】:Google Place Autocomplete not showing in Bootstrap modalGoogle Place Autocomplete 未在 Bootstrap 模式中显示
【发布时间】:2016-07-06 18:01:36
【问题描述】:

修复更新: 似乎我初始化盒子的方式引起了问题 这是修复它的原因

 <script>
    $(function () {
        var input = document.getElementById("keyword");
        var autocomplete = new google.maps.places.Autocomplete(input);

        $('#my-modal').modal('show');

    });

</script>
<style>
    .pac-container {
        z-index: 10000 !important;
    }
</style>

更新:我已将代码更新为非常简单,http://jsfiddle.net/g8vxmLn9/1/ 工作的原因是因为它使用较旧的引导程序和 jquery,所以我假设因为我使用的是较新的,所以我遇到了问题。

我只是想在 bootstrap(3.3) 模型中显示一个自动完成框,并让它在您键入时显示结果。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Test</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-2.2.2.min.js" integrity="sha256-36cp2Co+/62rEAAYHLmRCPIych47CvdM+uTBJwSzWjI=" crossorigin="anonymous"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places">   </script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<div>
    <div id="my-modal" class="modal" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <input name="keyword" id="keyword" type="text" />
            </div>

        </div>
    </div>
</div>
<script>
    $(function () {
        $("#my-modal").modal({
            show: false
        }).on("shown", function () {
            var input = document.getElementById("keyword");
            var autocomplete = new google.maps.places.Autocomplete(input);

        });
        $('#my-modal').modal('show');
    });
</script>
</body>
</html>

【问题讨论】:

  • id="map_canvas" 没有在 JS 中定义
  • @Shehary 我更新了代码,使其更简单并且与画布无关,仍然无法正常工作

标签: bootstrap-modal google-maps-api-2


【解决方案1】:

这解决了它

<script>
    $(function () {
        var input = document.getElementById("keyword");
        var autocomplete = new google.maps.places.Autocomplete(input);

        $('#my-modal').modal('show');

    });

</script>
<style>
    .pac-container {
        z-index: 10000 !important;
    }
</style>

【讨论】:

  • 这对我有用
  • 说明.pac-container 是 Google 自动完成结果的包装 div 元素。在这种情况下,模态已经有很高的z-index。所以你必须设置比模态更高的z-index
【解决方案2】:

只需添加

.pac-container {
    z-index: 10000 !important;
}

【讨论】:

    【解决方案3】:

    如果你还在寻找。

                        var pacContainerInitialized = false; 
                        $('#inputField').keypress(function() { 
                                if (!pacContainerInitialized) { 
                                        $('.pac-container').css('z-index', '9999'); 
                                        pacContainerInitialized = true; 
                                } 
                        }); 
    

    【讨论】:

      【解决方案4】:

      要与 Angular 2+ 一起使用,请确保使用 ng-deep

        ::ng-deep .pac-container
        {
          z-index: 99999 !important;
        }
      

      【讨论】:

        猜你喜欢
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-03-25
        • 2019-03-15
        • 2020-08-10
        • 2017-03-20
        • 2017-09-20
        • 2018-08-04
        相关资源
        最近更新 更多