如何更改Google自动填充的输入字段

时间:2016-05-27 13:57:02

标签: javascript jquery google-maps

如何更改autocomplete(Google Maps API)的输入字段?

autocomplete = new google.maps.places.Autocomplete(inputField, options);

function func1() {
 // switch element inputField to inputField2 (how?)
}

1 个答案:

答案 0 :(得分:0)

试试这个:

<script
src="https://maps.googleapis.com/maps/api/js?libraries=places">
</script>
<input type="text" id="loc1">
<input type="text" id="loc2">
<input type="text" id="loc3">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function initialize(){
var autocomplete1 = new google.maps.places.Autocomplete($("#loc1")[0], {});
var autocomplete2 = new google.maps.places.Autocomplete($("#loc2")[0], {});
var autocomplete3 = new google.maps.places.Autocomplete($("#loc3")[0], {});

google.maps.event.addListener(autocomplete1, 'place_changed', function () {
var place = autocomplete1.getPlace();
console.log(place.address_components);
});
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

我认为这会有所帮助:autofill。使用inputbox创建自动完成变量,并创建单个侦听器或多个侦听器。

我希望这会有所帮助