jQuery 下拉列表上的onchange
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2668574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onchange on dropdownlist
提问by Nick Kahn
my question is continuation of what i have asked see the link. Load Country/State/City
我的问题是我问过的问题的延续,请参阅链接。 加载国家/州/城市
i have expand to load my drop downs list from db and i just need a way to wire onchange method in my first dropdownlist and second, please see the code. appreciate any help.
我已经扩展以从 db 加载我的下拉列表,我只需要一种方法在我的第一个下拉列表和第二个下拉列表中连接 onchange 方法,请参阅代码。感谢任何帮助。
Append latest code:
附上最新代码:
<select id="country" onchange="getStateByCountryId()"></select> <br />
<select id="state"></select> <br />
$(document).ready(function() {
var options = {
type: "POST",
url: "SearchPage.aspx/LoadCountry",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
var returnedArray = msg.d;
country = $("#country");
country.append('<option>Select a Country</option>');
for (i = 0; i < returnedArray.length; i++) {
country.append("<option value='" + returnedArray[i].Id + "'>" + returnedArray[i].Name + "</option>");
}
}
};
$.ajax(options);
});
function getStateByCountryId() {
$("#country").change(function()
{
var _selected = $("#country").val();
var options =
{
type: "POST",
url: "SearchPage.aspx/StateBy",
data: "{'countryId':'" + _selected + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#state').empty();
var returnedArray = msg.d;
state = $("#state");
for (var i = 0; i < returnedArray.length; ++i) {
state.append("<option value='" + returnedArray[i].Id + "'>" + returnedArray[i].Name + "</option>");
}
}
};
$.ajax(options);
});
}
but does not populate? the way i am doing is that how you suppose to do?
但不填充?我正在做的就是你想怎么做?
thanks.
谢谢。
回答by Graza
$("#state").change(function(){
//on-change code goes in here.
//variable "this" references the state dropdown element
});
回答by derek
$(this).parent("select").attr("id");