$input.disabled = true;

or

$input.disabled = "disabled";

哪一种是标准方式?相反,如何启用禁用的输入?


当前回答

禁用:

$('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.

其他回答

像这样使用,

 $( "#id" ).prop( "disabled", true );

 $( "#id" ).prop( "disabled", false );

禁用输入字段的另一种方法是使用jQuery和css,如下所示:

jQuery("#inputFieldId").css({"pointer-events":"none"})

并且为了实现相同的输入,代码如下:

jQuery("#inputFieldId").css({"pointer-events":""})

禁用:

$('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.

如果您只想反转当前状态(如切换按钮行为):

$("input").prop('disabled', ! $("input").prop('disabled') );

2018年更新:

现在不需要jQuery了,因为document.querySelector或document.querySelectedAll(对于多个元素)做的工作与$几乎完全相同,加上更明确的getElementById、getElementsByClassName、getElementByTagName

禁用“输入复选框”类的一个字段

document.querySelector('.input-checkbox').disabled = true;

或多个元素

document.querySelectorAll('.input-checkbox').forEach(el => el.disabled = true);