$input.disabled = true;

or

$input.disabled = "disabled";

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


当前回答

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

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

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

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

其他回答

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

$("input").prop('disabled', ! $("input").prop('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.

您可以将其放在代码中的全局位置:

$.prototype.enable = function () {
    $.each(this, function (index, el) {
        $(el).removeAttr('disabled');
    });
}

$.prototype.disable = function () {
    $.each(this, function (index, el) {
        $(el).attr('disabled', 'disabled');
    });
}

然后你可以写一些东西,比如:

$(".myInputs").enable();
$("#otherInput").disable();

2018,无JQuery(ES6)

禁用所有输入:

[...document.querySelectorAll('input')].map(e => e.disabled = true);

禁用id=“my input”的输入

document.getElementById('my-input').disabled = true;

问题是JQuery,它只是仅供参考。

像这样使用,

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

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