禁用Asp.net单选按钮列表中的其他单选按钮(如果其中一个已选中)

Disable other RadioButtons in an Asp.net RadioButtonList if one among them is selected

本文关键字:单选按钮 Asp 一个 net 其他 禁用 如果 列表      更新时间:2023-09-26

当选择其中一个时,我需要禁用其他asp.net单选按钮。有三个单选按钮。例如,如果选择了"现金"单选按钮,则应禁用列表项中的其他两个按钮。我做了一些研究,当第三个被选中时,我确实禁用了另外两个。但在我的代码隐藏页上,它为我选择的单选按钮提供了空值。这是我的代码:

My.aspx

 <div class="col-sm-4" style="padding-top:4px ; margin-top:-8px">
    <asp:RadioButtonList runat="server" ID="loanTypeCheck" CssClass="pull-right" RepeatDirection="Horizontal">
    <asp:ListItem Value="Cash" Text="&nbsp;&nbsp;Cash&nbsp;&nbsp;"/>
    <asp:ListItem Value="Non-Cash" Text="&nbsp;&nbsp;Non-Cash&nbsp;&nbsp;" />
    <asp:ListItem Value="Dharauti" Text="&nbsp;&nbsp;Dharauti&nbsp;&nbsp;" />
      
    </asp:RadioButtonList>
</div>

我的代码隐藏

protected void SaveButton_Click(object sender, EventArgs e)
{
    if (IsFormValidForSave())
    {
        var loanReceipt = GetLoanReceiptFromForm();
        var transactionsDetails = new List<TransactionDetail>();
        if (loanTypeCheck.SelectedValue == "Cash")
        {
           //
        }
    }
}

这是我禁用单选按钮的JavaScript

$('input[type=radio]').change(function () {
    $("#<%=loanTypeCheck.ClientID   %>").find('input').prop('disabled', true);
});

UI中一切正常,但我在代码背后的loanTypeCheck.SelectedValue中得到了null值。

您可以使用:not(:checked)选择器禁用未选择的单选按钮:

$('#<%= loanTypeCheck.ClientID %>').change(function () {
    $(this).find('input[type=radio]:not(:checked)').prop('disabled', true);
});

保持选中的单选按钮处于启用状态将允许将所选值过帐回。