您有两种方法,您可以在客户端执行此操作,也可以在服务器端执行此操作,具体取决于您的需要:
服务器端解决方案:您需要将AutoPostBack
RadioButtonList 的属性添加为 true,并且divs
将runat
属性设置为server
标记:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="SearchByName" Value="1"></asp:ListItem>
<asp:ListItem Text="SearchByDate" Value="2"></asp:ListItem>
<asp:ListItem Text="SearchByValue" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<div id="myDiv1" runat="server" visible="false">Div Content</div>
后面的代码:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedValue =="2")
this.myDiv1.Visible = true;
else
this.myDiv1.Visible = false;
}
客户端解决方案:
Javascript:
window.onload = function () {
var inputs = document.getElementsByTagName("input");
if (inputs.length > 0) {
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "radio") {
inputs[i].onclick = function () {
if (this.value == "2") {
document.getElementById("div1").style.display = "block";
}
else {
document.getElementById("div1").style.display = "none";
}
}
}
}
}
}
标记:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" >
<asp:ListItem Text="SearchByName" Value="1"></asp:ListItem>
<asp:ListItem Text="SearchByDate" Value="2"></asp:ListItem>
<asp:ListItem Text="SearchByValue" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<div id="div1" style="display:none">Div Content</div>
当然,这将获得页面中的所有单选按钮,因此您可能需要确保获得正确的单选按钮。