![](/img/trans.png)
[英]Getting Javascript code to fire on a selection change on a DropDownList in ASP.Net
[英]Javascript/ASP.net fire an event AFTER dropdownlist change
我有一個三個下拉列表國家,城市和地區,並且在一個html頁面上有初始空白選擇值和谷歌地圖。 當用戶從下拉列表中選擇位置值時,我試圖居中並放大地圖。
所以這是一個我遇到問題的場景:
我嘗試使用onchange並更改javascript事件,但兩者都導致從下拉列表中檢索的文本的值是在事件發送空文本(初始空白文本)到谷歌地圖之前的文本值。
那么,如何在下拉列表發生變化后觸發事件?
這是我的代碼:
<asp:DropDownList ID="DropDownList_Country" runat="server" Height="29px" Width="209px"
Font-Size="18px" CausesValidation="false" onchange="MapLocationUpdater();">
function MapLocationUpdater() {
var address = getDropdownListAddress();
geocoder.geocode({ 'address': address },
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(11);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
}
);
}
function getDropdownListAddress() {
var ddlCountry = document.getElementById("<%=DropDownList_Country.ClientID%>");
var ddlCity = document.getElementById("<%=DropDownList_City.ClientID%>");
var ddlDistrict = document.getElementById("<%=DropDownList_District.ClientID%>");
var CountryTxt = ddlCountry.options[ddlCountry.selectedIndex].text;
var CityTxt = ddlCity.options[ddlCity.selectedIndex].text;
var DistrictTxt = ddlDistrict.options[ddlDistrict.selectedIndex].text;
return CountryTxt + " " + CityTxt + " " + DistrictTxt;
}
試試這個:
<asp:DropDownList ID="DropDownList_Country" runat="server" Height="29px" Width="209px"
Font-Size="18px" CausesValidation="false" onchange="setTimeout('MapLocationUpdater();',1000);">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.