【问题标题】:How to enable and disable drop down list by selecting radio button in ASP.NET如何通过在 ASP.NET 中选择单选按钮来启用和禁用下拉列表
【发布时间】:2016-09-07 12:41:21
【问题描述】:

我想创建一个页面,每当我选择一个特定的单选项目时,应该启用一个下拉列表,如果它选择了其他项目,则应该禁用下拉列表。

这是我在 ASP.NET 中的表单脚本

<form id="ticketbooking" class="book-ticket" runat="server">
    <asp:Label ID="mode" runat="server" class="label">Mode of Travel</asp:Label><br />
    <asp:RadioButton name="selector" ID="train" GroupName="travelmode" runat="server" value="tr"></asp:RadioButton>
    <asp:Label ID="forTrain" for="train" runat="server" class="label">Train</asp:Label> 
    <div class="check"></div>
    <asp:RadioButton name="selector" ID="air" GroupName="travelmode" runat="server" value="ar" OnCheckedChanged="air_CheckedChanged"></asp:RadioButton>
    <asp:Label ID="forAir" for="air" runat="server" class="label">Air</asp:Label> 
    <div class="check"></div><br />
    <asp:Label ID="airname" runat="server" class="label">Airline Name</asp:Label><br />
    <asp:DropDownList class="droplist" ID="airlist" runat="server" Enabled="false" Font-Size="20px">
        <asp:ListItem>Air India</asp:ListItem>
        <asp:ListItem>Kingfisher</asp:ListItem>
        <asp:ListItem>Jet Airways</asp:ListItem>
        <asp:ListItem>Spice Jet</asp:ListItem>
    </asp:DropDownList>
</form>

我尝试了以下几个脚本,但它们不起作用...

方法一:

<script>
    $(document).ready(function () {
        $('input[name="selector"]').click(function () {
            if ($('input[name="selector"]').is(':checked')) {
                var radioValue = $("input[name='selector']:checked").val();
                if (radioValue == "tr") {
                    $("#airlist").prop("disabled", true);
                } else if (radioValue == "ar") {
                    $("#airlist").prop("disabled", false);
                }
            }
        });
    });
</script>

方法二:

protected void air_CheckedChanged(object sender, EventArgs e)
{
    if (air.Checked == true)
    {
        airlist.Enabled = true;
    }
    else
    {
        airlist.Enabled = false;
    }
}

我都试过了,但都不起作用..当我点击“Air”单选按钮时,下拉列表应该被启用..我做错了什么吗?需要一些建议。我还是 ASP.NET 的新手,不知道如何在这个框架中工作。

【问题讨论】:

    标签: javascript c# jquery html asp.net


    【解决方案1】:

    对于方法 1,把 ClientIDMode="static" 你的 JavaScript 代码可以正常工作,就像

    <asp:RadioButton ClientIDMode="static" name="selector" ID="train" GroupName="travelmode" runat="server" value="tr"></asp:RadioButton>
    

    对于方法 2 将 AutoPostback=true 你的 C# 将正常工作就像

    <asp:RadioButton AutoPostback="true" name="selector" ID="train" GroupName="travelmode" runat="server" value="tr"></asp:RadioButton>
    

    【讨论】:

      【解决方案2】:

      对于方法 2,您需要将属性 AutoPostback 添加到 Aspx 页面

      AutoPostback=true
      

      【讨论】:

        猜你喜欢
        • 2019-05-04
        • 2020-10-18
        • 2015-05-28
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-21
        • 2020-10-18
        相关资源
        最近更新 更多