【发布时间】:2014-03-24 10:18:33
【问题描述】:
我有级联下拉菜单。在选择一个下拉列表时,另一个下拉列表使用 ajax 绑定。这完全有效,但我希望第二个下拉列表也应该包含默认文本和值,如('选择我')。我怎样才能添加这个默认值。这是我的代码。
function BindCities(drpstate) {
var stateid = $(drpstate).val();
$.ajax({
url: '/Register.aspx/BindCities',
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{"id":"' + stateid + '"}',
success: function (result) {
var drpcity = $('.drpcity');
drpcity.empty();
$.each(result.d, function () {
drpcity.append(
$('<option/>', {
value: this.id,
text: this.name
})
);
});
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
}
HTML:
<asp:DropDownList runat="server" CssClass="drpstate" onchange="BindCities(this)" ID="drpdwnState">
<asp:ListItem Text="Select State" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" CssClass="drpcity" ID="drpdwnCity">
<asp:ListItem Text="Select City" Value="0"></asp:ListItem>
</asp:DropDownList>
服务器端代码:
[WebMethod]
public static List<CountryState> BindCities(int id)
{
EkbnManager em = new EkbnManager();
List<City> cities = em.GetCityByStateId(id);
List<CountryState> countrystate = new List<CountryState>();
foreach (var item in cities)
{
CountryState cnt = new CountryState();
cnt.name = item.Name;
cnt.id = item.Id;
countrystate.Add(cnt);
}
return countrystate;
}
【问题讨论】: