javascript 单击后使按钮不可点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23724639/
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
Make a button unclickable after one click
提问by user3650090
I'm trying to create a button that becomes deactivated after the first click; however I have no clue how to do it. This is what I have now:
我正在尝试创建一个在第一次单击后停用的按钮;但是我不知道该怎么做。这就是我现在所拥有的:
<input type="button" id="btnSearch" value="Search" onclick="Fuction();" />
This button will NOT submit a form. Could someone help me? I'm totally new to this. I've tried following the answer in other threads but got lost.
此按钮不会提交表单。有人可以帮助我吗?我对此完全陌生。我试过遵循其他线程中的答案,但迷路了。
回答by Frinsh
Separating javascript from HTML:
将 javascript 与 HTML 分开:
HTML:
HTML:
<input type="button" value="Click me!" id="form-button"/>
Javascript:
Javascript:
document.getElementById('form-button').onclick = function () {
this.disabled = true;
}
回答by Tommy Ivarsson
Not the preferred way of course, but this will get you the result you are after.
当然不是首选的方式,但这会让你得到你想要的结果。
function Fuction() {
document.getElementById('btnSearch').disabled = 'disabled';
}
回答by Pavlo
Set disabled
attribute in the end of your handler:
disabled
在处理程序的末尾设置属性:
function Fuction(button) {
// your code
button.disabled = true;
}
Markup:
标记:
<input type="button" id="btnSearch" value="Search" onclick="Fuction(this);" />
回答by nietonfir
Just call event.preventDefault()
in the functions implicitly added event variable to first stop the propagation. To disable it afterwards just call setAttribute("disabled", true);
on the element. Something like
只需调用event.preventDefault()
隐式添加的事件变量的函数即可首先停止传播。要在之后禁用它,只需调用setAttribute("disabled", true);
该元素即可。就像是
function aMeaningfulName(e) {
e.preventDefault();
e.target.setAttribute("disabled", true);
// your logic here
}