javascript - 无限的 Google Places 实例

标签 javascript jquery html location google-places-api

我的数据库记录被打印到一个表中,其中包含输入中的值。对于每条记录,我都需要由 Google 他们的 API 自动完成位置字段。

HTML:

<input name="edit_airplane[1][location]" type="text" class="airplane_location" value="Balmaceda">

Javascript:

function initAutocomplete() {
    var airplane_location_field = jQuery( '.airplane_location');

    autocomplete = new google.maps.places.Autocomplete( airplane_location_field ), {types : ['geocode']} );
    autocomplete.addListener( 'place_changed', function() {
        /* 
           Whenever the value of the location field has changed. I need to
           Create a new input field which is hidden with the full address 
           Of the selected google autocomplete location.
        */
    });
}

检查哪个输入字段已更改的最佳方法是什么?

最佳答案

做了很多研究,最终得出了这个解决方案。

function initAutocomplete() {
    // All of the airplanes_location input fields are combined.
    var airplane_location_fields = jQuery( '.airplane_location' );

    // For each location form field 
    airplane_location_fields.each( function() {
        var _this = jQuery( this ), autocomplete = new google.maps.places.Autocomplete( this );
        _this.data('autocomplete', autocomplete);

        // For each input field I stored the autocomplete object into .data; 
        _this.data('autocomplete').addListener( 'place_changed', function() {
            getPlaceData( _this );
        } );
    } );
}


function getPlaceData( input ) {
    // In this function I checked the input.data('autocomplete');
}

关于javascript - 无限的 Google Places 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653526/

相关文章:

javascript - LG Web OS 无法打开内部链接

javascript - 填写完所有先前的字段后,添加新的输入字段

javascript - .slideToggle(使用 Jquery)不工作?

html - CSS样式如何在一行中显示特定的元素符号

javascript - 如何动态定义 'hero' 中的 'neon-animated-pages' ?

Javascript Canvas 碰撞侧检测

javascript - 动态属性的外部

javascript - JQuery 选择器问题(我认为)

html - Twitter bootstrap 风格继承与 jinja2

html - 如何使用 Angular 表达式对齐无序列表显示中的内容?