0
我有幾個地址欄,其中第一個鏈接到Google Places API。因此,您開始在此框中輸入地址,然後當您點擊地址時,Google API將地址分配到其他地址字段。當用戶完成此操作時,他們點擊一個按鈕,動態收集表單中的所有信息。當頁面第一次加載時,所有的控件都是動態加載的,所以我不能使用固定值,因爲可能有一個實例沒有將這些字段加載到頁面上。未從輸入框中提取的值
當我收集數據時,從谷歌API填充的字段保持空白,但所有其他字段填充他們應該填寫的內容。
下面是將點擊事件和管理API功能的JS:
//#region API - Add Job
$('#addJob').click(function() {
// Setup the object to pass to API
var Job = {};
$(".form__input").each(function() {
var name = this.name;
var value = this.value;
Job[name] = value;
});
console.log(Job);
// Pass Job Object to the API
});
//#endregion API - Add Job
//#region Address Auto Complete
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{ types: ['geocode'] });
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
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;
}
}
}
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());
});
}
}
//#endregion Address Auto Complete
這幾乎就像輸入字段的沒有價值,但我可以看到它的存在實際上是在頁面上。生成的HTML代碼是:
<input name="CollectionAddress" class="form__input js-required" id="autocomplete" onfocus="geolocate()" type="text" placeholder="Collection Address" autocomplete="off">
<input name="CollectionAddressLine1" class="form__input js-required" id="street_number" placeholder="No.">
<input name="CollectionAddressLine2" class="form__input js-required" id="route" placeholder="Street">
<input name="CollectionAddressLine3" class="form__input js-required" id="locality" placeholder="City">
<input name="CollectionPostcode" class="form__input js-required" id="postal_code" placeholder="Postcode">
,輸出是從 '工作' 的對象是:
CollectionAddress: "123 Bradwel...",
CollectionAddressLine1: "",
CollectionAddressLine2: "",
CollectionAddressLine3: "",
CollectionPostcode: ""
代碼工作得很好,當我運行它時,https://jsfiddle.net/x0w8v4ta/ –
這不是使用谷歌的地方自動完成,雖然 –