我有一个过程,旨在将用户输入的连接单独地址字段更改为单个隐藏字段,供Google地图使用该引脚放置该位置。Javascript事件监听器 - 通过Javascript设置的“更改”值
负责连接的事件监听器运行正常,但是当我尝试将监听器链接到隐藏域以执行引脚布局时,该函数不会被执行。
这里是我使用的代码:
$(document).ready(function initMap() {
...
function join_address() {
var address = document.getElementById('id_form5-address1').value;
var city = document.getElementById('id_form5-city').value;
var state = document.getElementById('id_form5-state').value;
var zip = document.getElementById('id_form5-zip').value;
document.getElementById('id_jointAddress').value = address+", "+city+", "+state+", "+zip;
}
document.getElementById("id_form5-city").addEventListener("change", join_address);
document.getElementById("id_form5-state").addEventListener("change", join_address);
document.getElementById("id_form5-zip").addEventListener("change", join_address);
function codeAddress() {
console.log("function engaged")
var address = document.getElementById("id_jointAddress").value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
document.getElementById("id_jointAddress").addEventListener("change", codeAddress);
...
}
“功能从事”从来没有打印到控制台,所以我假设它是构建错误的听众,或者是放错了地方。任何人都可以协助
编辑:这是有问题的HTML对象:
<input type="hidden" name="jointAddress" id="id_jointAddress">
如果我取消隐藏元件,和类型直接在字段它触发监听器。我认为“更改”侦听器不会响应通过Javascript所做的更改。有谁知道一个解决方案,将为我工作?
什么类型的对象是'#id_jointAddress?' – stackoverfloweth
它返回了你的html对象_document.getElementById(“ id_jointAddress“)_? –
介意提供一个例子? [它似乎工作正常](http://jsfiddle.net/ofyo6rtk/)。 –