Postępowałem zgodnie z tym tutorial, aby automatycznie wypełnić miasto na podstawie adresu.Google Autofill API
Ale kiedy próbowałem go zaimplementować, nie otrzymuję pożądanej funkcjonalności. Chcę, aby po wypełnieniu adresu pole miasta było automatycznie wypełniane. Poniżej jest mój kod:
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">City</label>
<div class="col-md-3">
<input type="text" id="crack" name="City" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Area</label>
<div class="col-md-3">
<input type="text" id="ar" name="Area" class="form-control" >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text- input">Address</label>
<div class="col-md-3">
<input type="text" id="add" name="Address" class="form-control" >
</div>
</div>
Poniżej jest kod javascript
<script>
var autocomplete = new google.maps.places.Autocomplete($("#crack")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log(place.address_components);
});
</script>
<script>
var autocomplete = new google.maps.places.Autocomplete($("#add")[0], {});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
console.log(place.address_components);
});
var placeSearch, autocomplete;
var componentForm = {
crack: 'long_name',
};
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
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;
}
}
}
</script>
<?php include 'inc/template_end.php'; ?>
<script type="text/javascript">
function hi()
{
var ele=(document.getElementById("sla").value);
if(ele==1)
{
document.getElementById("g1").style.display="none";
}
else
{
document.getElementById("g1").style.display = "initial";
}
}
</script>
Czy moja odpowiedź była dla ciebie pomocna @Legendary_Hunter? – Madness