我正在尝试在多个类元素上使用Google自动填充,但它似乎无法正常工作。它在我引用一个ID时有效,如here所示,但my fiddle没有。到目前为止,这是我的代码:
$(document).ready(function () {
addressLookup();
});
function addressLookup() {
var address = document.getElementsByClassName('form-control booking address');
var options = {
componentRestrictions: {
country: 'uk'
}
};
new google.maps.places.Autocomplete(address, options);
}
答案 0 :(得分:3)
您需要使用上述类迭代元素集合并创建。
var address = document.getElementsByClassName('form-control booking address');
for(var i=0; i< address.length; i++){
new google.maps.places.Autocomplete(address[i], options);
}
答案 1 :(得分:0)
问题出在函数getElementsByClassName的结果中。对于AutoComplete,您需要一个元素,而不是一个数组。
function addressLookup() {
var address = document.getElementById('pickup');
var address2 = document.getElementById('destination');
var options = {
componentRestrictions: {
country: 'uk'
}
};
new google.maps.places.Autocomplete(address, options);
new google.maps.places.Autocomplete(address2, options);
}