我有一個過程,旨在將用戶輸入的連接單獨地址字段更改爲單個隱藏字段,供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/)。 –