当用户输入地址时,它应该将地址放入隐藏的输入字段中。 javascript不会触发并写入隐藏的输入字段的值。Javascript not initiating for getElementByID
我相信它可能是javascript/jquery没有以正确的顺序执行,或者我有版本冲突问题?
我有一个谷歌地址自动完成
var placeSearch, autocomplete;
// Need to add Lat/Long to populate field.
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name',
};
function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')), {
types: ['geocode']
});
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = "";
document.getElementById(component).disabled = true;
}
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;
}
}
document.getElementById("Latitude").value = place.geometry.location.lat();
document.getElementById("Longitude").value = place.geometry.location.lng();
}
google.maps.event.addDomListener(window, "load", initAutocomplete);
我有输入字段
<input type="hidden" value="" class="form-control" id="street_number" name="pnumb">
<input type="hidden" value="" class="form-control" id="route" name="paddy">
<input type="hidden" value="" class="form-control" id="locality" name="pcity">
<input type="hidden" value="" class="form-control" id="administrative_area_level_1" name="pstat">
<input type="hidden" value="" class="form-control" id="postal_code" name="pzip">
<input type="hidden" value="" class="form-control" id="Latitude" name="plat">
<input type="hidden" value="" class="form-control" id="Longitude" name="plong">
这是JavaScript的jQuery我有我的页面的顶部。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
这就是页面底部的内容。
<script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="../../bootstrap/js/bootstrap.min.js"></script>
<script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="../../plugins/fastclick/fastclick.js"></script>
<script src="../../dist/js/app.min.js"></script>
<script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.1.0/js/dataTables.responsive.js"></script>
我收到以下错误代码从控制台
1.) ReferenceError: google is not defined[Learn More]
2.) TypeError: document.getElementById(...) is null[Learn More]
我也有与试图利用日期选择问题。
我相信我有我的javascript/jquery执行不当吗?
任何想法;我很可怕的JavaScript/jQuery的
为什么你包括jQuery 3.1.1和jQuery 2.2.3? – Pointy
我的引导是关于2.2.3的relaint,其中谷歌地图显示无法正常工作,所以我扔在3.1.1 @Pointy – Kray
添加另一个版本的jQuery几乎永远不会让事情变得更好。 – Pointy