Javascript 禁用和启用 html 输入按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13831601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling and enabling a html input button
提问by k.ken
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. I tried setting it back to false but that didn't enable it.
如何在需要时禁用和启用它?我试过,disabled="disable"但启用它是一个问题。我尝试将其设置回 false 但这并没有启用它。
回答by pala?н
Using Javascript
使用 JavaScript
Disabling a html button
document.getElementById("Button").disabled = true;Enabling a html button
document.getElementById("Button").disabled = false;
禁用 html 按钮
document.getElementById("Button").disabled = true;启用 html 按钮
document.getElementById("Button").disabled = false;
Using jQuery
使用 jQuery
All versions of jQuery prior to 1.6
jQuery 1.6 之前的所有版本
Disabling a html button
$('#Button').attr('disabled','disabled');Enabling a html button
$('#Button').removeAttr('disabled');
禁用 html 按钮
$('#Button').attr('disabled','disabled');启用 html 按钮
$('#Button').removeAttr('disabled');
All versions of jQuery after 1.6
jQuery 1.6 之后的所有版本
Disabling a html button
$('#Button').prop('disabled', true);Enabling a html button
$('#Button').prop('disabled', false);
禁用 html 按钮
$('#Button').prop('disabled', true);启用 html 按钮
$('#Button').prop('disabled', false);
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.
PS 根据jquery 1.6.1 的变化更新了代码。作为建议,始终使用最新的 jquery 文件和prop()方法。
回答by Ian
Since you are disabling it in the first place, the way to enable it is to set its disabledproperty as false.
由于您首先禁用它,启用它的方法是将其disabled属性设置为false.
To change its disabledproperty in Javascript, you use this:
要disabled在 Javascript 中更改其属性,请使用以下命令:
var btn = document.getElementById("Button");
btn.disabled = false;
And obviously to disable it again, you'd use trueinstead.
显然要再次禁用它,你会true改用。
Since you also tagged the question with jQuery, you could use the .propmethod. Something like:
由于您还使用 jQuery 标记了问题,因此您可以使用该.prop方法。就像是:
var btn = $("#Button");
btn.prop("disabled", true); // Or `false`
This is in the newer versions of jQuery. The older way to do this is to add or remove an attribute like so:
这是在较新版本的 jQuery 中。较旧的方法是添加或删除属性,如下所示:
var btn = $("#Button");
btn.attr("disabled", "disabled");
// or
btn.removeAttr("disabled");
The mere presence of the disabledproperty disables the element, so you cannot set its value as "false". Even the following should disable the element
该disabled属性的存在会禁用该元素,因此您不能将其值设置为“false”。即使以下内容也应禁用该元素
<input type="button" value="Submit" disabled="" />
You need to either remove the attribute completely orset its property.
您需要完全删除该属性或设置其属性。
回答by Jon
You can do this fairly easily with just straight JavaScript, no libraries required.
只需直接使用 JavaScript 即可轻松完成此操作,无需任何库。
Enable a button
启用按钮
document.getElementById("Button").disabled=false;
Disable a button
禁用按钮
document.getElementById("Button").disabled=true;
No external libraries necessary.
不需要外部库。
回答by I wrestled a bear once.
the disable attribute only has one parameter. if you want to reenable it you have to remove the whole thing, not just change the value.
disable 属性只有一个参数。如果要重新启用它,则必须删除整个内容,而不仅仅是更改值。
回答by Hamid N K
$(".btncancel").button({ disabled: true });
Here 'btncancel' is the class name of the button.
这里的 'btncancel' 是按钮的类名。
回答by Kamil Kie?czewski
Without jQuery disable input will be simpler
没有jQuery禁用输入会更简单
Button.disabled=1;
function dis() {
Button.disabled= !Button.disabled;
}
<input id="Button" type="button" value="+" style="background-color:grey" onclick="Me();"/>
<button onclick="dis()">Toggle disable</button>
回答by JIYAUL MUSTAPHA
<!--Button Disable Script-->
<script type="text/javascript">
function DisableButton() {
document.getElementById("<%=btnSave.ClientID %>").disabled = true;
}
window.onbeforeunload = DisableButton;
</script>
<!--Button Disable Script-->
回答by Shalika
This will surely work .
这肯定会奏效。
To Disable a button
禁用按钮
$('#btn_id').button('disable');
To Enable a button
启用按钮
$('#btn_id').button('enable');
回答by My Gull Wheels On
Stick to php...
坚持使用php...
Why not only allow the button to appear once an above criteria is met.
为什么不仅在满足上述条件后才允许按钮出现。
<?
if (whatever == something) {
$display = '<input id="Button" type="button" value="+" style="background-color:grey" onclick="Me();"/>';
return $display;
}
?>

