禁用和启用html输入按钮
问题描述:
所以我有一个这样的按钮:
So I have a button like this:
<input id="Button" type="button" value="+" style="background-color:grey" onclick="Me();"/>
如何在需要时禁用和启用它?我已经尝试 disabled =禁用
但启用它是一个问题。我尝试将其设置为false,但是没有启用它。
How can I disable and enable it when I want? I have tried disabled="disable"
but enabling it back is a problem. I tried setting it back to false but that didn't enable it.
答
使用Javascript
-
禁用html按钮
Using Javascript
-
Disabling a html button
document.getElementById("Button").disabled = true;
-
启用html按钮
-
Enabling a html button
document.getElementById("Button").disabled = false;
-
1.6之前的所有jQuery版本
-
禁用html按钮
Disabling a html button
$('#Button').attr('disabled','disabled');
-
-
启用html按钮
-
Enabling a html button
$('#Button').removeAttr('disabled');
-
所有版本的1.6之后的jQuery
-
禁用html按钮
Disabling a html button
$('#Button').prop('disabled', true);
-
-
启用html按钮
-
Enabling a html button
$('#Button').prop('disabled', false);
-
PS根据 jquery 1.6.1更改更新了代码一>。作为建议,请始终使用最新的jquery文件和
prop()
方法。P.S. Updated the code based on jquery 1.6.1 changes. As a suggestion, always use the latest jquery files and the
prop()
method.
-