谷歌自动填充API适用于多个文本框

3
我正在尝试使用Google API的JavaScript来提供地址填写时的建议,希望有3个文本框都可以使用。但不幸的是,只有1个文本框“Base city”能够响应JavaScript,另外两个无法响应。
JavaScript

       <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
        <script>
            var autocomplete = new google.maps.places.Autocomplete($("#loc")[0], {});

            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                console.log(place.address_components);
            });
        </script>
  </script>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Base City</label>
<div class="col-md-3">
<input type="text" id="loc" name="City" class="form-control"  >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Base Location</label>
<div class="col-md-3">
<input type="text" id="loc" name="Location" class="form-control"  >
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="example-text-input">Work Location</label>
<div class="col-md-3">
<input type="text" id="loc" name="Work_Location" class="form-control"  >
</div>
</div>

2个回答

0
问题在于你有三个相同的id="loc"的div;然而,id应该是唯一的。因此,当你运行你的代码时,编译器只看到第一个id并使用它,不再进一步查找下一个。
为了解决这个问题,你需要给每个div分配不同的id,并为每个div创建自动完成变量。你可能也可以创建单独的监听器,但我留给你决定。

            var autocomplete1 = new google.maps.places.Autocomplete($("#loc1")[0], {});
            var autocomplete2 = new google.maps.places.Autocomplete($("#loc2")[0], {});
            var autocomplete3 = new google.maps.places.Autocomplete($("#loc3")[0], {});

            google.maps.event.addListener(autocomplete1, 'place_changed', function () {
                var place = autocomplete1.getPlace();
                console.log(place.address_components);
            });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
    <body>
        <div class="form-group">
            <label class="col-md-3 control-label" for="example-text-input">Base City</label>
            <div class="col-md-3">
                <input type="text" id="loc1" name="City" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-3 control-label" for="example-text-input">Base Location</label>
            <div class="col-md-3">
                <input type="text" id="loc2" name="Location" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-3 control-label" for="example-text-input">Work Location</label>
            <div class="col-md-3">
                <input type="text" id="loc3" name="Work_Location" class="form-control">
            </div>
        </div>
    </body>


0
function initialize() {
    //debugger;
    // Create the autocomplete object, restricting the search
    // to geographical location types.
    var clsname = document.getElementsByClassName('autocomplet');

    for (var i = 0; i < clsname.length; i++) {
        var id = clsname[i].id;
        autocomplete = new google.maps.places.Autocomplete(

        (document.getElementById(id)), {
            types: ['geocode']
        });
    }


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接