我正在使用google maps api。我正在尝试为输入框实现自动完成地址功能。但是问题在于页面上的所有内容似乎都可以正常工作,但在模式弹出窗口中却无法正常工作。
这是HTML
<div class="col-xs-5 col-sm-2 col-md-2">
<a href="javascript:void(0)" data-ng-click="addlocationpopup()">+New
Location</a>
</div> <!--button which calls popup -->
这是模式弹出窗口
<div class="modal right fade addlocation" id="myModal_5" tabindex="-1"
role="dialog" aria-labelledby="myModalLabel2" data-keyboard="false">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel1">ENTER LOCATION
DETAILS</h4>
</div>
<form name="locationForm" novalidate="novalidate" method="post">
<div class="modal-body">
<div class="row">
<div>
<input id="autocomplete" placeholder="Enter your address"
onFocus="geolocate()" type="text"></input>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script
src="https://maps.googleapis.com/maps/api/js?
key=mykey&libraries=places&callback=initAutocomplete" async defer>
</script>
<script>
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{types: ['geocode']});
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
控制器。js:
$scope.addlocationpopup = function(){
$(".addlocation").modal("show");
}
如果在页面中我放一个输入框并键入它可以正常工作。但是在模态内部它不起作用,这是当我在输入框内键入内容时未显示的地方。我不明白为什么在弹出菜单中它不起作用,因为脚本是页面加载时已经加载。谁能告诉我如何使其在模式弹出窗口中工作?
最终我解决了这个问题。我使用z-index css功能在输入框输入时显示自动完成地址,就像这样
<div class="modal right fade addlocation" id="myModal_5" tabindex="-1"
role="dialog" aria-labelledby="myModalLabel2" data-keyboard="false" style="z-index:2;">
现在,我可以看到有关地址的建议。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句