简体   繁体   English

未从输入框中提取值

[英]Value not extracted from input box

I have several address fields of which the first is linked to the Google Places API. 我有几个地址字段,其中第一个链接到Google Places API。 So you start to type the address in this box and then when you click the address the google API splits the address over the other address fields. 因此,您开始在此框中输入地址,然后当您单击该地址时,google API会将地址拆分为其他地址字段。 When the user has done this they click a button that gathers all of info from the form dynamically. 用户完成此操作后,他们将单击一个按钮,该按钮可动态收集表单中的所有信息。 When the page first loads all of the controls are loaded in dynamically so I can't use fixed values as there may be an instance where those fields are not loaded onto the page. 当页面第一次加载时,所有控件都是动态加载的,因此我不能使用固定值,因为在某些情况下可能不会将这些字段加载到页面上。

When I collect the data, the fields populated from the google API remain empty , but all the other fields populate with what they should. 当我收集数据时,从google API填充的字段保持为空,但所有其他字段均填充了应有的字段。

Here's the JS that adds the click event and manages the API functionality: 这是添加click事件并管理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

It's almost as of the input field has no value, but I can see it's there actually on the page. 几乎到输入字段为止,它都没有值,但是我可以看到它实际上在页面上。 The generated html code is: 生成的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">

And the output is from the 'Job' object is: 而来自“作业”对象的输出为:

CollectionAddress: "123 Bradwel...", 
CollectionAddressLine1: "", 
CollectionAddressLine2: "", 
CollectionAddressLine3: "", 
CollectionPostcode: ""

 $(document).ready(function(){ $('#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); $("#result").html(JSON.stringify(Job)); // Pass Job Object to the API }); 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()); }); } } initAutocomplete(); //#endregion Address Auto Complete }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&sensor=false"></script> <input name="CollectionAddress" class="form__input js-required" id="autocomplete" onfocus="" 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"> <button id="addJob">addJob</button> <div id="result"></div> 

This is working fine. 一切正常。 After clicking on "addJob" button you can get all the values. 单击“ addJob”按钮后,您可以获得所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM