Disable/enable an input with jQuery?

后端 未结 18 2731
礼貌的吻别
礼貌的吻别 2020-11-21 07:47
$input.disabled = true;

or

$input.disabled = \"disabled\";

Which is the standard way? And, conversely, how do yo

18条回答
  •  臣服心动
    2020-11-21 08:09

    There are many ways using them you can enable/disable any element :

    Approach 1

    $("#txtName").attr("disabled", true);
    

    Approach 2

    $("#txtName").attr("disabled", "disabled");
    

    If you are using jQuery 1.7 or higher version then use prop(), instead of attr().

    $("#txtName").prop("disabled", "disabled");
    

    If you wish to enable any element then you just have to do opposite of what you did to make it disable. However jQuery provides another way to remove any attribute.

    Approach 1

    $("#txtName").attr("disabled", false);
    

    Approach 2

    $("#txtName").attr("disabled", "");
    

    Approach 3

    $("#txtName").removeAttr("disabled");
    

    Again, if you are using jQuery 1.7 or higher version then use prop(), instead of attr(). That's is. This is how you enable or disable any element using jQuery.

提交回复
热议问题