【问题标题】:Disable dropdown list when radiobutton is checked?检查单选按钮时禁用下拉列表?
【发布时间】:2019-01-30 16:53:46
【问题描述】:

我的网络表单有 2 个控件,drpBloodType and rbUnknownBloodType

在选中按钮时,我需要禁用列表。 我试过了:

protected void rbUnknownBloodType_CheckedChanged(object sender, EventArgs e)
{
    drpBloodType.Enabled = false;
}

<script>
   $(function () {
      $('#rbUnknownBloodType').change(function () {
         if ($(this).is(':checked')) {
         $('#drpBloodType').attr('disabled', 'disabled');
         } else {
         $('#drpBloodType').removeAttr('disabled');
         }
      });
   });
</script>

但都没有用。

【问题讨论】:

  • 你能展示你的 HTML 吗?
  • 您的 jQuery 选择器可能找不到 $('#rbUnknownBloodType'),因为 ASP.NET 在按钮 ID 中添加了类似 ContentPlaceHolder1_ 的内容。检查您生成的 HTML。你应该使用一个复选框,这样用户也可以取消选中 unknown 字段。
  • NVM,我只需要启用 AutoPostBack。我很笨!

标签: c# asp.net visual-studio


【解决方案1】:

您需要将change 事件处理程序分配给与'#rbUnknownBloodType' 在同一组中的所有单选按钮。当单选按钮未选中时,单选按钮上的更改事件不会触发 - 行

$('#drpBloodType').removeAttr('disabled');

在你的代码中永远不会执行。

$(".test").change(function(){
  console.log(this.value, this.checked);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="test" type="radio" name="test" value="1"/>
<input class="test" type="radio" name="test" value="2"/>

嗯,我看了你问题中的图片。那里的复选框会更好吗?

【讨论】:

    【解决方案2】:

    如果您有以下 aspx 标记,您可以在代码隐藏中执行以下操作。注意 CheckBox 上AutoPostBack="true" 的用法。

    <asp:DropDownList ID="BloodType" runat="server">
        <asp:ListItem Text="Select..." Value=""></asp:ListItem>
        <asp:ListItem Text="A+" Value="A+"></asp:ListItem>
        <asp:ListItem Text="A-" Value="A-"></asp:ListItem>
        <asp:ListItem Text="B+" Value="B+"></asp:ListItem>
        <asp:ListItem Text="B-" Value="B-"></asp:ListItem>
        <asp:ListItem Text="O+" Value="O+"></asp:ListItem>
        <asp:ListItem Text="O-" Value="O-"></asp:ListItem>
        <asp:ListItem Text="AB+" Value="AB+"></asp:ListItem>
        <asp:ListItem Text="AB-" Value="AB-"></asp:ListItem>
    </asp:DropDownList>
    
    <asp:CheckBox ID="UnknownBloodType" OnCheckedChanged="UnknownBloodType_CheckedChanged" 
      runat="server" AutoPostBack="true" />
    

    代码隐藏。

    protected void UnknownBloodType_CheckedChanged(object sender, EventArgs e)
    {
        //set the dll to default
        BloodType.SelectedValue = "";
    
        //cast the sender back to a checkbox and disable the dll based on it's checked status
        BloodType.Enabled = !((CheckBox)sender).Checked;
    }
    

    或者 jquery 解决方案。这会保存一个 PostBack,但它会在 PostBack 之后失去禁用状态。 %= UnknownBloodType.ClientID %&gt; 仅适用于 aspx 页面本身。如果您不想使用它,请查看ClientIdMode=Static

    <script>
        $(function () {
            $('#<%= UnknownBloodType.ClientID %>').change(function () {
                var dll = $('#<%= BloodType.ClientID %>');
                if ($(this).is(':checked')) {
                    dll.val('');
                    dll.attr('disabled', 'disabled');
                } else {
                    dll.removeAttr('disabled');
                }
            });
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2020-10-18
      • 2021-09-29
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多