Javascript 未从输入框中提取值
我有几个地址字段,其中第一个链接到GooglePlacesAPI。因此,您开始在这个框中键入地址,然后当您单击该地址时,google API将该地址拆分为其他地址字段。当用户完成此操作后,他们会单击一个按钮,动态收集表单中的所有信息。当页面第一次加载时,所有控件都是动态加载的,因此我不能使用固定值,因为可能有一个实例没有将这些字段加载到页面上 当我收集数据时,从GoogleAPI填充的字段仍然是空的,但是所有其他字段都填充了它们应该填充的内容 下面是添加单击事件并管理API功能的JS:Javascript 未从输入框中提取值,javascript,jquery,html,Javascript,Jquery,Html,我有几个地址字段,其中第一个链接到GooglePlacesAPI。因此,您开始在这个框中键入地址,然后当您单击该地址时,google API将该地址拆分为其他地址字段。当用户完成此操作后,他们会单击一个按钮,动态收集表单中的所有信息。当页面第一次加载时,所有控件都是动态加载的,因此我不能使用固定值,因为可能有一个实例没有将这些字段加载到页面上 当我收集数据时,从GoogleAPI填充的字段仍然是空的,但是所有其他字段都填充了它们应该填充的内容 下面是添加单击事件并管理API功能的JS: //#r
//#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
$(文档).ready(函数(){
$('#addJob')。单击(函数(){
//设置要传递给API的对象
var Job={};
$(“.form_uuinput”)。每个(函数(){
var name=this.name;
var值=此值;
作业[名称]=值;
});
控制台日志(作业);
$(“#result”).html(JSON.stringify(Job));
//将作业对象传递给API
});
var placeSearch,自动完成;
变量组件形式={
街道编号:“短名称”,
路线:'long_name',
地点:'long_name',
邮政编码:“短名称”
};
函数initAutocomplete(){
//创建自动完成对象,将搜索限制为地理位置
//位置类型。
autocomplete=new google.maps.places.autocomplete(
(document.getElementById('autocomplete')),
{类型:['geocode']});
//当用户从下拉列表中选择地址时,填充该地址
//表单中的字段。
autocomplete.addListener('place\u changed',fillInAddress);
}
函数fillInAddress(){
//从自动完成对象获取位置详细信息。
var place=autocomplete.getPlace();
for(componentForm中的var组件){
document.getElementById(组件).value='';
document.getElementById(组件).disabled=false;
}
//从place details中获取地址的每个组件
//并在表单上填写相应的字段。
对于(变量i=0;i
addJob
当我运行它时,代码运行得很好,但这并没有使用Google places自动完成功能
<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: ""