jquery设置只读
使用 jQuery 设置只读属性
通过 jQuery 可以轻松为表单元素设置只读属性,以下是几种常见方法:
设置单个元素的只读属性
$('#inputId').prop('readonly', true);
设置多个元素的只读属性
$('.inputClass').prop('readonly', true);
移除只读属性
$('#inputId').prop('readonly', false);
使用 attr() 方法(旧版本兼容)
对于 jQuery 1.6 之前的版本,可以使用 attr() 方法:
$('#inputId').attr('readonly', 'readonly');
移除属性
$('#inputId').removeAttr('readonly');
动态切换只读状态
通过条件判断动态设置只读:

var isReadonly = true;
$('#inputId').prop('readonly', isReadonly);
注意事项
- prop() 方法更推荐用于布尔属性如 readonly、checked 等
- 该方法适用于 input、textarea 等表单元素
- 设置 readonly 后用户仍可聚焦元素,但无法修改内容






