【问题标题】:I want to enable the textbox if the value of the radiobtnlist is 1如果 radiobtnlist 的值为 1,我想启用文本框
【发布时间】:2020-02-15 22:50:47
【问题描述】:

如果 RadioButtonList 的值为零,我有这段代码可以禁用文本框。但它不起作用我不知道为什么。

<script type="text/javascript">
     $(function () {
            $("#RadioButtonList1").change(function() {
              var st = ("#RadioButtonList1").val();
              if (("#RadioButtonList1").val() == "1") {
                   $("#TextBox_ArrivalDate").prop("disabled",false);
              }else{
                  $("#TextBox_ArrivalDate").prop("disabled",true);
              }
            );
       });

</script>

这是我的 RadioButtonList

<asp:Label ID="type" Cssclass="label" runat="server" Text="Please Choose One"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" style="margin-left: 47px" Width="153px">
    <asp:ListItem Value="1">Round Trip</asp:ListItem>
    <asp:ListItem Value="0">One Way</asp:ListItem>
</asp:RadioButtonList>

【问题讨论】:

    标签: javascript c# jquery asp.net webforms


    【解决方案1】:

    希望我的 sn-p 能以任何方式帮助您。祝你有美好的一天!

    <html>
    
    <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script type="text/javascript">
        $(function() {
          $("[name=trip]").change(function() {
            var st = $("[name=trip]:checked").val();
            if (st == "1") {
              $("#TextBox_ArrivalDate").prop('disabled', false);
            } else {
              $("#TextBox_ArrivalDate").prop('disabled', true);
            }
          });
        })
      </script>
    </head>
    
    <body>
      <input type="radio" name="trip" value="1" checked> roundtrip<br>
      <input type="radio" name="trip" value="0"> one way<br>
      <input type="text" id="TextBox_ArrivalDate">
    </body>
    
    </html>

    【讨论】:

    • 它仍然无法正常工作。如果我选择任何一个收音机 btn,这将禁用文本框
    • 您好,您是否尝试过“运行代码 sn-p”按钮来查看结果?它实际上运行良好,我实际上经常使用这个 sn-p :)
    【解决方案2】:

    更改此行

    $("#RadioButtonList1").change(function() 到 $("").change(function().

    ASP.Net 以不同的格式呈现 RadioButton ID。您可以使用此语法访问客户端单选按钮的 id。

    【讨论】:

    • 我是否也必须更改访问文本框 id 的语法?
    • 是的,对于所有 Asp.Net 控件,要在客户端执行操作,使用 ID.ClientID 访问值总是好的。
    【解决方案3】:
     $(document).ready(function () {
                    $('#RadioButtonList1 input').change(function () {
                        debugger;
                        if ($(this).val() == 1) {
                            $('#TextBox_ArrivalDate').attr("enabled", "enabled");
                        } else {
                            $('#TextBox_ArrivalDate').attr("disabled", "disabled");
                        }
                    });
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 2022-12-06
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      相关资源
      最近更新 更多