You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:jQuery代码对TextBox生效但RadioButtonList禁用失效

RadioButtonList无法通过jQuery禁用/启用的解决办法

你的jQuery代码能正常控制ID为txt1的TextBox禁用/启用,但对ID为rdl的RadioButtonList无效,核心原因和解决办法如下:

问题代码

jQuery代码:

var chk = $('#chk1');
if (chk.prop('checked')) {
        $('#txt1').prop("disabled", false);
        $('#rdl').prop("disabled", false);
 }
else {
    $('#txt').prop("disabled", true); // 此处存在笔误,应为$('#txt1')
    $('#rdl').prop("disabled", true);
}

ASPX页面RadioButtonList代码:

<asp:RadioButtonList ID="rdl" runat="server"/>

原因分析

ASP.NET的RadioButtonList控件渲染到前端后,会生成一个容器(默认是<table>),控件的ID会被赋予这个容器元素。而disabled属性仅对表单控件(如<input>)生效,直接给容器设置disabled不会对内部的单选按钮产生作用。而TextBox是直接渲染为<input>元素,所以设置disabled能直接生效。

另外注意:你代码的else分支里写的是$('#txt'),这是笔误,应该改成$('#txt1'),否则TextBox的禁用功能也会失效。

解决方法

直接选择RadioButtonList容器内部的单选按钮元素,设置它们的disabled属性:

修改后的jQuery代码:

var chk = $('#chk1');
if (chk.prop('checked')) {
    $('#txt1').prop("disabled", false);
    $('#rdl input[type="radio"]').prop("disabled", false);
} else {
    $('#txt1').prop("disabled", true);
    $('#rdl input[type="radio"]').prop("disabled", true);
}

如果你的项目使用ASP.NET 4.0及以上版本,还可以给RadioButtonList设置ClientIDMode="Static"确保前端ID和后台ID一致(避免ASP.NET自动生成带前缀的ID),但核心还是要选择内部的单选按钮:

<asp:RadioButtonList ID="rdl" runat="server" ClientIDMode="Static"/>

内容的提问来源于stack exchange,提问作者user819774

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:17:23