检测单选按钮是否已禁用或启用

20
<asp:RadioButtonList ID="rdStatus" onclick="javacript:display();" runat="server" RepeatDirection="Vertical">
<asp:ListItem Text="Temporary Waiver" Value="T" Selected="True"></asp:ListItem>    
<asp:ListItem Text="Never Expires" Value="P"></asp:ListItem>    
<asp:ListItem Text="Expires after the close of:" Value="W"></asp:ListItem>
</asp:RadioButtonList>

function display()
{
    if(radiobuton is enabled)
          //code here
     else
          //code here
}

请提供一些检测单选按钮是否启用或禁用的方法。

你能分享生成的HTML而不是ASP代码吗? - Arun P Johny
2个回答

9

使用jQuery来附加到单选按钮列表的点击事件,然后使用jQuery的:enabled选择器,像这样:

$('#rdStatus').click(function () {
    if($(this).is(':enabled')) { 
        // Do enabled radio button code here 
    }
    else {
        // Do disabled radio button code here
    }
});

现在您可以删除单选按钮列表标记中的onclick属性,因为jQuery将会找到单选按钮列表(rdStatus)并为您连接单击事件。

<asp:RadioButtonList ID="rdStatus" runat="server" RepeatDirection="Vertical">

onclick="javacript:display();" 这里需要做什么修改? - Blossom
@Blossom - 更新了答案,使用jQuery事件处理程序代替使用onclick属性的标记。 - Karl Anderson

0

试试这个:

<asp:RadioButtonList ID="rdStatus"  runat="server" RepeatDirection="Vertical">
<asp:ListItem Text="Temporary Waiver" Value="T" Selected="True"></asp:ListItem>    
<asp:ListItem Text="Never Expires" Value="P"></asp:ListItem>    
<asp:ListItem Text="Expires after the close of:" Value="W"></asp:ListItem>
</asp:RadioButtonList>

$("#<%=rdStatus.ClientID%>").click(function(){
    if($(this).attr('enabled'))
    {
        $(this).removeattr('enabled');
        $(this).attr('disabled',true);
    }
    else
    {
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接