Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I have a button like this:

(所以我有一个像这样的按钮:)

<input id="Button" type="button" value="+" style="background-color:grey" onclick="Me();"/>

How can I disable and enable it when I want?

(我要如何禁用和启用它?)

I have tried disabled="disable" but enabling it back is a problem.

(我尝试过disabled="disable"但将其重新启用是一个问题。)

I tried setting it back to false but that didn't enable it.

(我尝试将其设置回false,但没有启用它。)

  ask by k.ken translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
231 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using Javascript(使用JavaScript)

  • Disabling a html button

    (禁用HTML按钮)

     document.getElementById("Button").disabled = true; 
  • Enabling a html button

    (启用html按钮)

     document.getElementById("Button").disabled = false; 
  • Demo Here

    (在这里演示)


Using jQuery(使用jQuery)

All versions of jQuery prior to 1.6

(jQuery 1.6之前的所有版本)

  • Disabling a html button

    (禁用HTML按钮)

     $('#Button').attr('disabled','disabled'); 
  • Enabling a html button

    (启用html按钮)

     $('#Button').removeAttr('disabled'); 
  • Demo Here

    (在这里演示)

All versions of jQuery after 1.6

(1.6之后的所有版本的jQuery)

  • Disabling a html button

    (禁用HTML按钮)

     $('#Button').prop('disabled', true); 
  • Enabling a html button

    (启用html按钮)

     $('#Button').prop('disabled', false); 
  • Demo Here

    (在这里演示)

PS Updated the code based on jquery 1.6.1 changes .

(PS更新了基于jquery 1.6.1更改的代码。)

As a suggestion, always use the latest jquery files and the prop() method.

(建议,始终使用最新的jquery文件和prop()方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...