单击后如何禁用 html 按钮而不是提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19025677/
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
how to disable a html button after it has been clicked not a submit button
提问by Pazrat
hi i have a html button and when you press it, it should give you the distances of shops from where you are so when you press it they appear now the problem is because it is in early stages the information is printed everytime the button is pressed i would just like to now how to disable it when it has been pressed once and then reactivate it when another button has been pressed this is not a submit button my code for the button is :
嗨,我有一个 html 按钮,当你按下它时,它应该给你商店与你所在位置的距离,所以当你按下它时,它们现在出现问题是因为它处于早期阶段,每次按下按钮时都会打印信息我现在只想知道如何在按下一次时禁用它,然后在按下另一个按钮时重新激活它这不是提交按钮我的按钮代码是:
<button onclick="getLocation()">Search</button>
any help would be much appreciated thanks in advance
任何帮助将不胜感激提前感谢
回答by Sasidharan
I think its very clear for you..
我想这对你来说很清楚..
Call your getlocation() method inside the click event..
在点击事件中调用您的 getlocation() 方法..
Code
代码
$( "#bind" ).click(function() {
$(this).attr("disabled", "disabled");
$("#unbind").removeAttr("disabled");
});
$( "#unbind" ).click(function() {
$(this).attr("disabled", "disabled");
$("#bind").removeAttr("disabled");
});
Output
输出
回答by Satyam Saxena
Do like that:
这样做:
<script type="text/javascript">
var clicked = false;
function disableMe() {
if (document.getElementById) {
if (!clicked) {
document.getElementById("myButton").value = "thank you";
clicked = true;
return true;
} else {
return false;
}
} else {
return true;
}
}
</script>
<button type="button" id="myButton" onClick="return disableMe()" value="OK">
Or, you can do this: onclick="this.disabled=true"
或者,您可以这样做: onclick="this.disabled=true"
回答by Marco Lau
There are a number of ways to do this. Usually, you could use JavaScript to edit the attributes. It's best to read on this and ask if you have problems with your coding.
有多种方法可以做到这一点。通常,您可以使用 JavaScript 来编辑属性。最好阅读此内容并询问您的编码是否有问题。
You should find what you need on this post: How to disable html button using JavaScript?
您应该在这篇文章中找到您需要的内容:如何使用 JavaScript 禁用 html 按钮?
回答by D.Alexander
You could try something like this:
你可以尝试这样的事情:
$('button').click(function(){
// Add functionality here, then...
$(this).attr("disabled", true);
});
回答by Daniel Varab
This can be solved in several ways, but the most intuitive way would most likely be to feed the application (which this appears to be) which state it is in. Such as:
这可以通过多种方式解决,但最直观的方式很可能是向应用程序(似乎是)提供它所处的状态。例如:
var locationSearched = false //global scope of the application
getLocation() {
if(locationSearched) {
//do nothing or inform user of the state + wait for alternative button click
} else {
//execute the method and request to retrieve the location
state = true
}
}
Alternatively this could be passed as a parameter to the method. This does seem abit wonky in my opinion tho. With the solution suggested you can perform logical behavior in relation to the state, making it abit more scalable.
或者,这可以作为参数传递给方法。在我看来,这确实有点奇怪。使用建议的解决方案,您可以执行与状态相关的逻辑行为,使其更具可扩展性。