删除Content Place Holder中的单选按钮列表单选按钮

时间:2015-01-22 12:23:55

标签: javascript jquery html asp.net ajax

这在带有正文表单标记的普通网页上是可行的,但是当我在母版页内容占位符中应用它时,它无法按名称获取元素。谁能帮我吗。非常感谢。

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        hideRadioSymbol();  
    });

    function hideRadioSymbol() {
        var rads = new Array();
        var rads1 = new Array();
        rads = document.getElementsByName('RadioButtonList1'); //Whatever ID u have given to ur radiolist.
        rads1 = document.getElementsByName('RadioButtonList2');

        for (var i = 0; i < rads.length; i++)
            document.getElementById(rads.item(i).id).style.display = 'none'; //hide
        for (var i = 0; i < rads1.length; i++)
            document.getElementById(rads1.item(i).id).style.display = 'none'; //hide
    }

</script>


        <input type="button" data-name="hide" value="Toggle" id="toggle2" class="btnMenu blueMenu"/>
        <div id="sidebar2">
            <asp:RadioButtonList ID="RadioButtonList1" runat="server"  CssClass="ph-buttonMenu ph-btn-greyMenu"  >
                    <asp:listitem>s1</asp:listitem>
                    <asp:listitem>s2</asp:listitem>
                    <asp:listitem>s3</asp:listitem>
                    <asp:listitem>s4</asp:listitem>
            </asp:RadioButtonList>
                   </div>
        <br />
        <input type="button" data-name="hide" value="Toggle" id="toggle3"  class="btnMenu blueMenu"/>
        <div id="sidebar3">
            <asp:RadioButtonList ID="RadioButtonList2" runat="server" CssClass="ph-buttonMenu ph-btn-greyMenu"  >
                    <asp:listitem>s1</asp:listitem>
                    <asp:listitem>s2</asp:listitem>
                    <asp:listitem>s3</asp:listitem>
                    <asp:listitem>s4</asp:listitem>
            </asp:RadioButtonList>
        </div>


</asp:Content>

1 个答案:

答案 0 :(得分:-1)

使用此ClientID,如下所示,获取控件的ID而不是Name
rads = document.getElementsById(&#39;&lt;%= RadioButtonList1.ClientID%&gt;&#39;)
rads1 = document.getElementsById(&#39;&lt;%= RadioButtonList2.ClientID%&gt;&#39;);

您可以隐藏容器而不是单个元素。好像你必须隐藏RadioButtonList1,然后隐藏身份div的{​​{1}},如下所示

sidebar2

因为你正在使用jquery。