$input.disabled = true;

or

$input.disabled = "disabled";

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


当前回答

这对我有用

$("#values:input").attr("disabled",true);
$("#values:input").attr("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.

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

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

and

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

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

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

然后像这样使用

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

方法4(这是野生编码器答案的扩展)

txtName.disabled=1//0启用<input id=“txtName”>

<html>
<body>

Name: <input type="text" id="myText">



<button onclick="disable()">Disable Text field</button>
<button onclick="enable()">Enable Text field</button>

<script>
function disable() {
    document.getElementById("myText").disabled = true;
}
function enable() {
    document.getElementById("myText").disabled = false;
}
</script>

</body>
</html>