如何检查按钮是否被使用JavaScript

问题描述:

点击我只是好奇,如果有一个简单的方法来做到这些方针的东西:如何检查按钮是否被使用JavaScript

的javascript:

if(document.getElementById('button').clicked == true) 
{ 
    alert("button was clicked"); 
} 

HTML:

<input id="button" type="submit" name="button" value="enter"/> 

您可以添加一个click事件处理程序是:

document.getElementById('button').onclick = function() { 
    alert("button was clicked"); 
}​;​ 

这会在点击时发出警报,如果您想稍后跟踪它,只需在该功能中将变量设置为true,而不是通知即可,或者如果要计算点击次数(无论您的最终用途是什么),请将其设置为variable++You can see an example here

只是挂钩onclick事件:

<input id="button" type="submit" name="button" value="enter" onclick="myFunction();"/> 
+10

请不要使用在线处理,[有这么多原因不这样做](http://stackoverflow.com/questions/2479557/why-is-it-bad-practice-to-use-links-with-the-javascript-protocol/2479582#2479582)。 – 2010-05-07 11:39:01

这将做到这一点

<input id="button" type="submit" name="button" onclick="myFunction();" value="enter"/> 

<script> 
function myFunction(){ 

alert("You button was pressed"); 

}; 

</script> 

希望帮助