简体   繁体   English

仅在同时选择两个无线电组时启用按钮

[英]Enable button only when both radio groups are selected

I'm using ASP.NET controls. 我正在使用ASP.NET控件。 In my form, I have 4 radio buttons. 在我的表单中,我有4个单选按钮。 One of the radio buttons group names is book the other one is card. 单选按钮组名称之一是书,另一个是卡。 My goal is to disable the next button if none of the radio buttons is clicked or if only one of the groups is clicked. 我的目标是,如果没有单击任何单选按钮或仅单击了其中一个组,则禁用下一个按钮。 To enable the next button the user must select from one of the book options and one of the card options. 要启用下一个按钮,用户必须从一个书籍选项和一个卡片选项中进行选择。 How can I accomplish this? 我该怎么做?

Visual 视觉效果

在此处输入图片说明

ASP.NET - HTML ASP.NET-HTML

<div style="padding:30px; background-color:antiquewhite;">

    <div style="background-color:aliceblue; width:190px; padding:10px;">
        <asp:RadioButton ID="RadioPassBookYes" runat="server" Text="Book-Yes" GroupName="book"/>
        <asp:RadioButton ID="RadioPassBookNo" runat="server" Text="Book-No" GroupName="book"/>
    </div>
    <br>
    <div style="background-color:ActiveBorder; width:190px;  padding:10px;">
        <asp:RadioButton ID="RadioPassCardYes" runat="server" Text="Card-Yes" GroupName="card"/>
        <asp:RadioButton ID="RadioPassCardNo" runat="server" Text="Card-No" GroupName="card"/>
    </div>

    <br>
    <asp:Button ID="BtnNextRecentInfo" runat="server" Text="Next" disabled="disabled"/>
    &nbsp;
    <asp:Button ID="btnCheck" runat="server" Text="Check" />
</div>

JS Script JS脚本

<script type="text/javascript">

        var isCheckedPassBookYes = $("#<%=RadioPassBookYes.ClientID%>").prop('checked');
        var isCheckedPassBookNo = $("#<%=RadioPassBookNo.ClientID%>").prop('checked');
        var isCheckedPassCardYes = $("#<%=RadioPassCardYes.ClientID%>").prop('checked');
        var isCheckedPassCardNo = $("#<%=RadioPassCardNo.ClientID%>").prop('checked');


        $("#<%=RadioPassBookYes.ClientID%>").click(function () {
        });
        $("#<%=RadioPassBookNo.ClientID%>").click(function () {
            isCheckedPassBookNo = true
        });
        $("#<%=RadioPassCardYes.ClientID%>").click(function () {
        });
        $("#<%=RadioPassCardNo.ClientID%>").click(function () {
            isCheckedPassCardNo = true
        });


        if (isCheckedPassBookYes === true) {
            $("#<%=BtnNextRecentInfo.ClientID%>").attr("disabled", "disabled");
        }

        if (isCheckedPassBookNo === true) {
            $("#<%=BtnNextRecentInfo.ClientID%>").removeAttr("disabled");
        }
</script>

This might just work : 这可能工作:

<script type="text/javascript">


            $("#<%=RadioPassBookYes.ClientID%>").click(function () {
                check_selection();
            });
            $("#<%=RadioPassBookNo.ClientID%>").click(function () {
               check_selection();
            });
            $("#<%=RadioPassCardYes.ClientID%>").click(function () {
                check_selection();
            });
            $("#<%=RadioPassCardNo.ClientID%>").click(function () {
                check_selection();
            });

            check_selection();

            function check_selection()
            {
                var isCheckedPassBookYes = $("#<%=RadioPassBookYes.ClientID%>");
                var isCheckedPassBookNo = $("#<%=RadioPassBookNo.ClientID%>");
                var isCheckedPassCardYes = $("#<%=RadioPassCardYes.ClientID%>");
                var isCheckedPassCardNo = $("#<%=RadioPassCardNo.ClientID%>");

                if(
                   (isCheckedPassBookNo.is(':checked') || isCheckedPassBookYes.is(':checked'))
                   && (isCheckedPassCardYes.is(':checked') || isCheckedPassCardNo.is(':checked'))
                    )
                    {

                        $("#<%=BtnNextRecentInfo.ClientID%>").removeAttr("disabled");
                    }
                    else
                    {
                        $("#<%=BtnNextRecentInfo.ClientID%>").attr("disabled", "disabled");
                    }

            }           
    </script>

Sorry but I had to add an answer that uses a RadioButtonList instead of 2 RadioButtons for one group, uses the aspnet Enabled property for the button and is using about 1/4th of the javascript lines as the accepted answer. 抱歉,但我必须添加一个答案,该答案使用RadioButtonList而不是一组2个RadioButtons,对按钮使用aspnet Enabled属性,并且使用大约1/4的javascript行作为接受的答案。

<asp:RadioButtonList ID="RadioPassBook" runat="server">
    <asp:ListItem Text="Book-Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="Book-No" Value="0"></asp:ListItem>
</asp:RadioButtonList>

<asp:RadioButtonList ID="RadioPassCard" runat="server">
    <asp:ListItem Text="Card-Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="Card-No" Value="0"></asp:ListItem>
</asp:RadioButtonList>

<asp:Button ID="BtnNextRecentInfo" runat="server" Text="Next" Enabled="false" />

<script type="text/javascript">
    $("#<%= RadioPassBook.ClientID %>, #<%= RadioPassCard.ClientID %>").change(function () {
        CheckBothRadioButtonLists();
    });

    function CheckBothRadioButtonLists() {
        if ($("input[name='<%= RadioPassBook.UniqueID %>']:checked").val() == null || $("input[name='<%= RadioPassCard.UniqueID %>']:checked").val() == null) {
            $("#<%= BtnNextRecentInfo.ClientID %>").attr("disabled", "disabled");
        } else {
            $("#<%= BtnNextRecentInfo.ClientID %>").removeAttr("disabled");
        }
    }
</script>

And here the snippet if you want to use a aspnet Validator. 如果您想使用aspnet验证程序,请在此处摘录。

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="CheckBothRadioButtonLists" ErrorMessage="Both lists have to be selected"></asp:CustomValidator>

<script type="text/javascript">
    function CheckBothRadioButtonLists(sender, element) {
        if ($("input[name='<%= RadioPassBook.UniqueID %>']:checked").val() == null || $("input[name='<%= RadioPassCard.UniqueID %>']:checked").val() == null) {
            element.IsValid = false;
        } else {
            element.IsValid = true;
        }
    }
</script>

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

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