简体   繁体   中英

About RadioButtonList in ASP.NET

I have a RadioButtonList . I want the button to be available when I click "Agree", and not available when I click "Disagree". Default is disabled.

Now whether I agree or disagree, my btnSubmit button will become available. And it won't change back to unavailable.

function acceptprotocol() {
    if (document.getElementById("rblAccept").SelectValue == 0 {
        document.getElementById("btnSubmit").disabled = true;
    }
    else {
        document.getElementById("btnSubmit").disabled = false;
    }
}

RadioButtonList:

<asp:RadioButtonList ID="rblAccept" runat="server" 
     RepeatDirection="Horizontal" style="margin:auto" 
     onclick="acceptprotocol()">
    <asp:ListItem Value="0">Agree</asp:ListItem>
    <asp:ListItem Value="1" Selected="True">Against</asp:ListItem>
</asp:RadioButtonList>

How to solve it? Please help me.

Here's the code snippet for your solution:

RadioButtonList:

<asp:RadioButtonList ID="rblAccept" runat="server" RepeatDirection="Horizontal" style="margin:auto"
 onclick="acceptprotocol()">
<asp:ListItem Value="0">Agree</asp:ListItem>
<asp:ListItem Value="1" Selected="True">Disagree</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Enabled="false" />

Function:

function acceptprotocol() {
        var radioButtonAccept = document.getElementById('<%=rblAccept.ClientID %>').getElementsByTagName("input");;
        for (var i = 0; i < radioButtonAccept.length; i++) {
            if (radioButtonAccept[i].checked) {
                if (radioButtonAccept[i].value == 1) {
                    document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
                }
                else {
                    document.getElementById('<%=btnSubmit.ClientID %>').disabled = false;
                    //use this bellow line if you want the after enable button button will availble all time
                    document.getElementById('<%=rblAccept.ClientID %>').removeAttribute("onclick");
                }
                break;
            }
            else {
                document.getElementById('<%=btnSubmit.ClientID %>').disabled = true;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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