简体   繁体   English

选择单选按钮时启用单击按钮

[英]Enable click button when a radio button is selected

I have a list of radio buttons generated dynamically by a list view. 我有一个由列表视图动态生成的单选按钮列表。 The user selects one radio button and click the Update button to update his data. 用户选择一个单选按钮,然后单击“更新”按钮以更新其数据。 I want to keep the button disabled until the user makes his selection. 我想让按钮保持禁用状态,直到用户做出选择为止。 How can I accomplish that? 我该怎么做?

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID_BG" DataSourceID="SqlDataSource_BGlist">
            <ItemTemplate>
                <itemtemplate>                
            <label><input id="Radio1" name="BG_list" type="radio" runat="server" value='<%# Eval("BG_fileName") %>'/>
                <img alt="" style="width:150px" src="/Members/images/BG/icons/<%# Eval("BG_fileName") %>"></label>                 
        </itemtemplate> ....



    <asp:Button disabled="disabled" CssClass="btn btn-default" ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Change" OnClientClick="return confirm('Are you sure you want to change your Background Image?');" />

Try this: bind change event to radio button using .on() as radio buttons generated dynamically and enable update button inside it. 尝试以下操作:使用.on()作为动态生成的单选按钮,将change事件绑定到单选按钮,并在其中启用更新按钮。

$(function(){
 $(document).on('change','input[name="BG_list"]',function(){
    $('#UpdateButton').removeAttr('disabled');
 });
});

EDIT - use update button id like $("#<%=UpdateButton.ClientID %>").removeAttr('disabled'); 编辑 -使用更新按钮ID,例如$("#<%=UpdateButton.ClientID %>").removeAttr('disabled'); as mentioned in @pid answer. 如@pid答案中所述。 As I am not asp.net developer so don't know about this that actual id could be different. 由于我不是asp.net开发人员,因此不知道实际ID可能有所不同。

Ciao Gloria, Ciao Gloria,

You should add jQuery and then this piece of code in the header: 您应该添加jQuery,然后在标头中添加以下代码:

$(function () {
  $("input[id*='Radio1']").click(function () {
    $("#<%=UpdateButton.ClientID %>").removeAttr('disabled');
  });
});

To have ASP.NET supplanting <%=UpdateButton.ClientID %> with the actual ID of the control, you must enable the head as a server-side control, because ASP.NET will otherwise not check the content of <head> neither nested <script> tags: 若要使ASP.NET用控件的实际ID <%=UpdateButton.ClientID %> ,必须将head作为服务器端控件,因为ASP.NET否则将不检查也不嵌套的<head>的内容<script>标签:

<head runat="server">
  <script>

    // server-side variant JS with ASP.NET <%= tags %>

  </script>
</head>

More about this topic here: Understanding the runat server attribute 有关此主题的更多信息,请参见了解runat服务器属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM