Google Places API:如何使用jQuery选择pac-container类?

时间:2018-07-23 08:27:57

标签: javascript jquery google-maps

为什么我无法选择pac-container班。

我正在使用Google Places API

请参见以下pac-container类的屏幕截图:-

enter image description here

尝试了什么:-

$(document).on('click', '.pac-container', function() {
    alert('pac container class selected');
})

1 个答案:

答案 0 :(得分:0)

我得到了这个问题的答案,实际上我在document.ready内部调用了我的Jquery代码,但是我需要在Place Autocomplete代码内部使用它。

Place Autocomplete在加载有问题的我的自定义jquery之前被加载,但是自定义jquery应该与Place Autocomplete一起加载。

现在将我的代码带入一个函数中:-

function ZipOrCityOnMobile() {
    if ($(window).width() < 768) {
        $('.m-search-close').hide();
        $('[name="ZipOrCity"]').parents('li').removeClass('search-control-mobile');
        $('.home-text').css('z-index', '9');
    }
}

然后在Place Autocomplete

内部调用
function initOfficeLocationAddLocationPicker() {
        var input = document.getElementById('ZipOrCity');
        var places = new google.maps.places.Autocomplete(input);
         // Set country for us.
        places.setComponentRestrictions({ 'country': ['us'] });
        google.maps.event.addListener(places, 'place_changed', function () {
            var address = AddressHepler(places.getPlace());
            if (address.postal_code != null && address.postal_code != '' && address.postal_code != undefined) {
                var CityStateandZipcode = address.locality.concat(', ', address.state, ', ', address.postal_code);
                $("#ZipOrCity").val(CityStateandZipcode);
            }
            else if (address.locality != null && address.locality != '' && address.locality != undefined) {
                var CityandState = address.locality.concat(', ', address.state);
                $("#ZipOrCity").val(CityandState);
            }
            else {
                $("#ZipOrCity").val(address.state);
            }
            ZipOrCityOnMobile();
        });
    }