我在 Twitter Bootstrap 模式对话框中有一个 Google 地图自动完成输入字段,但不显示自动完成结果。但是,如果我按向下箭头键,它会选择下一个自动完成结果,因此看起来自动完成代码工作正常,只是结果显示不正确。也许它隐藏在模式对话框后面?
这是屏幕截图:
在自动完成字段中输入内容不会产生任何结果
按向下箭头键给出第一个结果
还有代码:
<div class="modal hide fade" id="map_modal">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal">×</button>
<div class="control-group">
<label class="control-label" for="keyword">Cari alamat :</label>
<div class="controls">
<input type="text" class="span6" name="keyword" id="keyword">
</div>
</div>
<div id="map_canvas" style="width:530px; height:300px"></div>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
<script type="text/javascript">
$("#map_modal").modal({
show: false
}).on("shown", function()
{
var map_options = {
center: new google.maps.LatLng(-6.21, 106.84),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), map_options);
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-6, 106.6),
new google.maps.LatLng(-6.3, 107)
);
var input = document.getElementById("keyword");
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo("bounds", map);
var marker = new google.maps.Marker({map: map});
google.maps.event.addListener(autocomplete, "place_changed", function()
{
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(15);
}
marker.setPosition(place.geometry.location);
});
google.maps.event.addListener(map, "click", function(event)
{
marker.setPosition(event.latLng);
});
});
</script>
我已经尽力自己解决这个问题,但是由于我不知道自动完成结果的html和css(检查元素没有给出任何内容),所以我现在迷失了。有什么帮助吗?
先谢谢了!
问题在于
z-index
的
.modal
使用此 CSS 标记:
.pac-container {
background-color: #FFF;
z-index: 20;
position: fixed;
display: inline-block;
float: left;
}
.modal{
z-index: 20;
}
.modal-backdrop{
z-index: 10;
}
您还可以查看最终演示这里
ps: 感谢 @lilina 在 jsfiddle.com 上的演示
Bootstrap 的
.modal
z-index
默认为 1050。
jQuery UI 的
.ui-autocomplete
z-index
默认为 3。
所以把它放在CSS上:
/* Fix Bootstrap .modal compatibility with jQuery UI Autocomplete,
see http://stackoverflow.com/questions/10957781/google-maps-autocomplete-result-in-bootstrap-modal-dialog */
.ui-autocomplete {
z-index: 1051 !important;
}
创造奇迹! :)
与 Bootstrap 3 一起使用:
.pac-container {
z-index: 1040 !important;
}
在我的场景中,.pac-container 的 z-index 值将被初始化并覆盖为 1000,即使我在 CSS 中设置也是如此。 (可能是通过谷歌API?)
我的肮脏修复
$('#myModal').on('shown', function() {
$(".pac-container").css("z-index", $("#myModal").css("z-index"));
}
我花了 2.3 个小时来找出 google place API 下拉 z 索引的错误。 最后我找到了这个。 只需将此代码粘贴到 JS 脚本顶部并更新输入 ID 名称即可。
var pacContainerInitialized = false;
$("#inputField").keypress(function() {
if (!pacContainerInitialized) {
$(".pac-container").css("z-index", "9999");
pacContainerInitialized = true;
}
});
完整参考链接。 https://groups.google.com/forum/#!topic/google-maps-js-api-v3/GZX0ynQNn1M 感谢高塔姆。
一般来说,您只需将 .pac-container z-index 提高到 1050 以上,就不需要其他 CSS 覆盖。
我发现 jQuery UI 对话框也存在这个问题。因此,如果它最终对其他人有帮助,这里有一个关于 Bootstrap/jQuery UI 模式在 z 平面中的位置的快速参考:
jQuery UI Dialog - z-index: 1001
Bootstrap Modal - z-index: 1050
这对我有用。
var pacContainerInitialized = false;
$('#inputField').keypress(function() {
if (!pacContainerInitialized) {
$('.pac-container').css('z-index','9999');
pacContainerInitialized = true;
}
});
如果您使用的是 Visual Studio,请在
style.css
页面上将 z-index
的 .modal
设置为 20。
例如
.modal {
z-index: 20 !important;
}
引导5:
.pac-container {
z-index: 1060 !important;
}
找到页面或对话框的 z-index,然后添加以下 css 属性:
.pac-container { z-index: [大于您的 z-index] !important; }
这将解决问题。