$input.disabled = true;
or
$input.disabled = "disabled";
哪一种是标准方式?相反,如何启用禁用的输入?
$input.disabled = true;
or
$input.disabled = "disabled";
哪一种是标准方式?相反,如何启用禁用的输入?
当前回答
像这样使用,
$( "#id" ).prop( "disabled", true );
$( "#id" ).prop( "disabled", false );
其他回答
禁用:
$('input').attr('readonly', true); // Disable it.
$('input').addClass('text-muted'); // Gray it out with bootstrap.
启用:
$('input').attr('readonly', false); // Enable it.
$('input').removeClass('text-muted'); // Back to normal color with bootstrap.
我使用@gnarf答案并将其添加为函数
$.fn.disabled = function (isDisabled) {
if (isDisabled) {
this.attr('disabled', 'disabled');
} else {
this.removeAttr('disabled');
}
};
然后像这样使用
$('#myElement').disable(true);
$("input")[0].disabled = true;
or
$("input")[0].disabled = false;
对输入类型禁用true:
如果是特定的输入类型(例如文本类型输入)
$("input[type=text]").attr('disabled', true);
对于所有类型的输入类型
$("input").attr('disabled', true);
2018年更新:
现在不需要jQuery了,因为document.querySelector或document.querySelectedAll(对于多个元素)做的工作与$几乎完全相同,加上更明确的getElementById、getElementsByClassName、getElementByTagName
禁用“输入复选框”类的一个字段
document.querySelector('.input-checkbox').disabled = true;
或多个元素
document.querySelectorAll('.input-checkbox').forEach(el => el.disabled = true);