$input.disabled = true;

or

$input.disabled = "disabled";

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


当前回答

像这样使用,

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

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

其他回答

$("input")[0].disabled = true;

or

$("input")[0].disabled = false;

只是为了新的惯例,并使其适应未来(除非ECMA6(????)发生了巨大变化):

$(document).on('event_name', '#your_id', function() {
    $(this).removeAttr('disabled');
});

and

$(document).off('event_name', '#your_id', function() {
    $(this).attr('disabled','disabled');   
});

像这样使用,

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

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

我使用@gnarf答案并将其添加为函数

   $.fn.disabled = function (isDisabled) {
     if (isDisabled) {
       this.attr('disabled', 'disabled');
     } else {
       this.removeAttr('disabled');
     }
   };

然后像这样使用

$('#myElement').disable(true);

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

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