$input.disabled = true;

or

$input.disabled = "disabled";

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


当前回答

您可以使用jQuery prop()方法动态禁用或启用表单元素或控件。prop()方法需要jQuery1.6及更高版本。

例子:

<script type="text/javascript">
        $(document).ready(function(){
            $('form input[type="submit"]').prop("disabled", true);
            $(".agree").click(function(){
                if($(this).prop("checked") == true){
                    $('form input[type="submit"]').prop("disabled", false);
                }
                else if($(this).prop("checked") == false){
                    $('form input[type="submit"]').prop("disabled", true);
                }
            });
        });
    </script>

其他回答

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

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

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

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

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

$.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();

禁用:

$('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")[0].disabled = true;

or

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