如何使用JavaScript检查是否单击了Button [英] How to check whether a Button is clicked by using JavaScript

查看:145
本文介绍了如何使用JavaScript检查是否单击了Button的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇是否有一种简单的方法可以按照这些方式做点什么:

I'm just curious if there is a simple way to do something along these lines:

javascript:

javascript:

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

html:

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


推荐答案

您可以为此添加点击事件处理程序:

You can add a click event handler for this:

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

如果你想跟踪它,它会在点击时发出警告以后,只需在该函数中将变量设置为true而不是警告,或者变量++ 如果您想计算点击次数,无论您的最终用途是什么。 您可以在此处查看示例

This will alert when it's clicked, if you want to track it for later, just set a variable to true in that function instead of alerting, or variable++ if you want to count the number of clicks, whatever your ultimate use is. You can see an example here.

这篇关于如何使用JavaScript检查是否单击了Button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆