0

我有与下拉列表相关的复选框列表和与单选按钮组相关的复选框。代码示例是这样的:

  <table class="style1">
        <tr>
            <td class="style10" colspan="6">
                </td>
        </tr>
        <tr>
            <td class="style6">
                <asp:CheckBox ID="chkBoxChanl" AutoPostBack="false" 
                    runat="server" Text="Channel" 
                    CssClass="ChkBoxStyle" Font-Size="Small" 
                     />&nbsp;</td>
            <td class="style7">
                <asp:DropDownList ID="ddlChannel" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style6">
                 <asp:CheckBox ID="chkBoxParty" runat="server" Text="Party" 
                     CssClass="ChkBoxStyle" Font-Size="Small" />&nbsp;</td>
            <td class="style7">
                <asp:DropDownList ID="ddlParty" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style13">
                <asp:CheckBox ID="chkBoxContntTyp" runat="server" Font-Bold="True" 
                    Text="Content Type" CssClass="ChkBoxStyle" Font-Size="Small"/>&nbsp;</td>

            <td class="style8" rowspan="2">
                <asp:RadioButton ID="rdBtnNews" runat="server" Text="News" CssClass="RadioGroup" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                <asp:RadioButton ID="rdBtnTicker" runat="server" Text="Ticker" CssClass="RadioGroup" />&nbsp;&nbsp;<asp:RadioButton ID="rdBtnBreakingNews"
                            runat="server" Text="Breaking News" CssClass="RadioGroup" />
                </br></br><asp:RadioButton ID="rdBtnBeeper" runat="server" Text="Beeper" CssClass="RadioGroup" />&nbsp;&nbsp;<asp:RadioButton 
                    ID="rdBtnTalkshow" runat="server" Text="TalkShow" CssClass="RadioGroup" /></td>
        </tr>
        <tr>
            <td class="style6">
                <asp:CheckBox ID="chkBoxDate" runat="server" Font-Bold="True" Text="Date" 
                    CssClass="ChkBoxStyle" Font-Size="Small" />&nbsp;</td>
            <td class="style7">
                <asp:TextBox ID="tbFrom" runat="server" CssClass="tbDate"></asp:TextBox><asp:Label ID="Label5"
                    runat="server" Text="To"></asp:Label><asp:TextBox ID="tbTo"
                    runat="server" CssClass="tbDate"></asp:TextBox>&nbsp;</td>
            <td class="style6">
                <asp:CheckBox ID="chkBoxPerson" runat="server" Font-Bold="True" Text="Person" 
                    CssClass="ChkBoxStyle" Font-Size="Small" />&nbsp;</td>
            <td class="style7">
                <asp:DropDownList ID="ddlPerson" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style13">
                </td>
        </tr>
        <tr>
            <td class="style4">
                <asp:CheckBox ID="chkBoxProgrm" runat="server" Font-Bold="True" 
                    Text="Program" CssClass="ChkBoxStyle" Font-Size="Small"/>&nbsp;</td>
            <td class="style3">
                <asp:DropDownList ID="ddlProgram" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style5">
                <asp:CheckBox ID="chkBoxProvince" runat="server" Font-Bold="True" 
                    Text="Province" CssClass="ChkBoxStyle" Font-Size="Small"/>&nbsp;</td>
            <td class="style3">
                <asp:DropDownList ID="ddlProvince" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style18">
                <asp:CheckBox ID="chkContentTone" runat="server" Font-Bold="True" 
                    Text="Content Tone" CssClass="ChkBoxStyle" Font-Size="Small"/>&nbsp;</td>
            <td class="style9">
                <asp:RadioButton ID="rdBtnPositive" runat="server" Text="Positive" CssClass="RadioGroup" />&nbsp;&nbsp;<asp:RadioButton 
                    ID="rdBtnNegative" runat="server" Text="Negative" CssClass="RadioGroup" />&nbsp;&nbsp;<asp:RadioButton ID="rdBtnNeutral"
                            runat="server" Text="Neutral" CssClass="RadioGroup" />&nbsp;</td>
        </tr>
        <tr>
            <td class="style14">
                <asp:Label ID="Label4" runat="server" Text="Keyword" Font-Names="Verdana,Arial" 
                    Font-Bold="True" Font-Size="Small"></asp:Label>&nbsp;</td>
            <td class="style15">
                <asp:TextBox ID="tbKeyword" runat="server" CssClass="tb"></asp:TextBox>&nbsp;</td>
            <td class="style14">
                <asp:CheckBox ID="chkBoxCity" runat="server" Font-Bold="True" Text="City" 
                    CssClass="ChkBoxStyle" Font-Size="Small"/>&nbsp;</td>
            <td class="style15">
                <asp:DropDownList ID="ddlCity" runat="server" CssClass="ddl">
                </asp:DropDownList>&nbsp;</td>
            <td class="style16">
                </td>
            <td class="style17">
                <asp:Button ID="Button1" runat="server" Text="Search" CssClass="button" />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button2"
                    runat="server" Text="Report" CssClass="button" /></td>
        </tr>       
    </table>

如下所示。 在此处输入图像描述

但我想在页面加载时禁用所有下拉列表和单选按钮,当我选中复选框时,只启用相关的下拉列表或复选框组。像这样。 在此处输入图像描述 当我选中复选框(内容类型)时,单选按钮组会像这样启用。 在此处输入图像描述 我希望当我选中任何复选框时,相关的下拉列表或单选按钮组就会启用。我想要的所有这些都在客户端(服务器端是我已经知道的不好的做法)java脚本。任何想法,因为我从过去 2 天开始搜索,但没有找到任何结果!

4

1 回答 1

0

这是标记:

<asp:CheckBox ID="CheckBox1" CssClass="toggleCheckBox" runat="server" 
     data-associatedTo="<%# TextBox1.ClientID %>" />
<asp:TextBox ID="TextBox1" runat="server" />
<br />
<asp:CheckBox ID="CheckBox2" CssClass="toggleCheckBox" runat="server" 
     data-associatedTo="radioButtionsPanel1" />
<div id="radioButtionsPanel1">
     <asp:RadioButtonList ID="RadioButtonList1" runat="server">
          <asp:ListItem Text="First" />
          <asp:ListItem Text="Second" />
          <asp:ListItem Text="Third" />
     </asp:RadioButtonList>
</div>
<br />
<asp:CheckBox ID="CheckBox3" CssClass="toggleCheckBox" runat="server" 
     data-associatedTo="<%# DropDownList1.ClientID %>" />
<asp:DropDownList ID="DropDownList1" runat="server">
     <asp:ListItem Text="First" />
     <asp:ListItem Text="Second" />
     <asp:ListItem Text="Third" />
</asp:DropDownList>

我曾使用数据绑定表达式来设置data-associatedTo属性值。如果您愿意,您可以将此属性添加到代码隐藏中的复选框,例如在Page_PreRender事件处理程序中。

这是javascript:

<script type="text/javascript">
     $(function () {
          $(".toggleCheckBox[data-associatedTo]").each(function () {
               enabeDisableAssociatedControl(this); // disable associated controls
               $(this).on("click", function () { 
                     enabeDisableAssociatedControl(this); });
          });
     });

     function enabeDisableAssociatedControl(checkBox) {
          var targetControl = $("#" + $(checkBox).attr("data-associatedTo"));
          if (targetControl) {
               var checked = $(":checkbox", checkBox).is(":checked");
               targetControl.find("*").andSelf().each(function () { this.disabled = !checked; });
          }
     }
</script>
于 2012-10-19T11:07:24.233 回答