我正在寻找用于javascript的示例代码,以检查项目是否在下拉列表中被选中。然后让它可见。
答案 0 :(得分:2)
我愿意:(用jquery):
$(function () {
$("#<%= AspControlName.ClientID %>").change(function () {
if ($(this).val() == "TheValueYouWant") {
$("#WhatToShowID").show();
} else {
$("#WhatToHideID").hide();
}
});
});
答案 1 :(得分:1)
如果您想在代码中使用C#执行此操作:
示例HTML:
<asp:DropDownList ID="DropDownList1" autopostback="true" runat="server"> </asp:DropDownList>
C#代码
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Make sure everything is hidden
item_1_extra_stuff.Visible = false;
item_2_extra_stuff.Visible = false;
switch (DropDownList1.Text) {
case "Item 1":
item_1_extra_stuff.Visible = true;
break;
case "Item 2":
item_2_extra_stuff.Visible = true;
break;
}
}
回发允许下拉列表触发该事件,然后case语句只检查当前输入并运行show / hides所需。