我有以下 javascript 代码,它在作为地址的一部分的不同输入文本框上执行谷歌地图自动完成。
function startAutoComplete() {
var address = document.getElementById("addressId");
var city = document.getElementById("cityId");
var state = document.getElementById("stateId");
var zip = document.getElementById("zipId");
//option for autocomplete
var option = {types: ['geocode'], componentRestrictions: {country: 'us'}};
//autocomplete for address textbox
autocomplete = new google.maps.places.Autocomplete(address, option);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
//parses the result object and populates the other textboxes accordingly
for(i=0; i<place.address_components.length; i++)
{
if(place.address_components[i].types[0] == 'locality')
city.value = place.address_components[i].long_name;
if(place.address_components[i].types[0] == 'administrative_area_level_1')
state.value = place.address_components[i].short_name;
if(place.address_components[i].types[0] == 'postal_code')
zip.value = place.address_components[i].long_name;
}
/* Here is the PROBLEM */
address.value = place.name;
});
但是,当我尝试使用完整地址的缩短版本(所以这里只有街道号码和街道名称)更新正在自动完成的文本框时。完整地址文本框中的值不会改变。我尝试使用 jquery 并在文本框中使用 setAttribute() 设置 value 属性。我究竟做错了什么?