RadioButtonList 的 OnSelectedIndexChanged 事件未触发

sam*_*iaj 5 asp.net radiobuttonlist selectedindexchanged

我在 asp.net 中有一个 RadioButtonList。我正在尝试做一件简单的事情,即在更改列表中的单选按钮时显示或隐藏 div。但事件 OnSelectedIndexChanged 没有触发。我不明白原因。这是代码

<asp:RadioButtonList ID="CityStateZip" runat="server" ForeColor="#333300" AutoPostBack="true" RepeatDirection="Horizontal"  Width="274px" CausesValidation="true" ValidationGroup="SchoolSearchGroup"  OnSelectedIndexChanged="CityStateZip_SelectedIndexChanged">
                <asp:ListItem  Value="1" Text="City and State" />
                <asp:ListItem Value="2" Text="Zip Code" />
</asp:RadioButtonList>
<div id="zipcodediv" runat="server" visible="false" style="margin-left:75px">
 code.........
</div>
<div id="citystatediv" runat="server" style="margin-left:75px; width: 708px;">
code........
</div>
Run Code Online (Sandbox Code Playgroud)

背后的代码

 protected void CityStateZip_SelectedIndexChanged(Object sender,EventArgs e)
    {           
        if (CityStateZip.SelectedValue == "1")
        {               
            zipcodediv.Visible = false;
            citystatediv.Visible = true;
        }
        if (CityStateZip.SelectedValue == "2")
        {                
            citystatediv.Visible = false;
            zipcodediv.Visible = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sat*_*ngh 2

这就是您可以在客户端执行的操作。
在 head 标签中添加JQuery脚本文件,并在此处添加您的 javascript 函数函数名称 (selectValue)
测试代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">

        function selectValue() {

            if (document.getElementById("CityStateZip_0").checked == true) {

                $("#divOne").show(100);
                $("#divTwo").hide(100);

            }

            if (document.getElementById("CityStateZip_1").checked == true) {

                $("#divOne").hide(100);
                $("#divTwo").show(100);

            } 
        }
    </script>
Run Code Online (Sandbox Code Playgroud)

Html 标记:

  <div>
        <asp:RadioButtonList ID="CityStateZip" runat="server" onchange="return selectValue();"
            RepeatDirection="Horizontal">
            <asp:ListItem Value="1" Selected="True">City and State</asp:ListItem>
            <asp:ListItem Value="2">Zip Code</asp:ListItem>

        </asp:RadioButtonList>
    </div><br /><br />
        <div id="divOne">
            <h3>Div one...</h3>
            Enter your City State content
        </div>

        <div id="divTwo">
            <h3>Div two...</h3>
            Enter you Zip code content
        </div>
Run Code Online (Sandbox Code Playgroud)