$input.disabled = true;

or

$input.disabled = "disabled";

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


当前回答

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

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

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

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

其他回答

对输入类型禁用true:

如果是特定的输入类型(例如文本类型输入)

$("input[type=text]").attr('disabled', true);

对于所有类型的输入类型

$("input").attr('disabled', true);

禁用:

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

您可以使用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")[0].disabled = true;

or

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

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

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