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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2788191/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:55:46  来源:igfitidea点击:

How to check whether a Button is clicked by using JavaScript

javascriptdom-events

提问by Ruth

Is there a simple way to do something along these lines:

有没有一种简单的方法可以按照以下方式做某事:

JavaScript:

JavaScript:

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

HTML:

HTML:

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

回答by Nick Craver

You can add a click event handler for this:

您可以为此添加一个单击事件处理程序:

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

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.

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

回答by Sabba Keynejad

This will do it

这将做到

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

<script>
function myFunction(){
    alert("You button was pressed");
};   
</script>

回答by Paolo

Just hook up the onclick event:

只需连接 onclick 事件:

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

回答by ecc521

Try adding an event listener for clicks:

尝试为点击添加一个事件监听器:

document.getElementById('button').addEventListener("click", function() {
   alert("You clicked me");
}?);?

Using addEventListeneris probably a better idea then setting onclick- onclickcan easily be overwritten by another piece of code.

使用addEventListener可能是比设置更好的主意onclick-onclick很容易被另一段代码覆盖。

You can use a variable to remember if the button has been clicked before:

您可以使用变量来记住该按钮之前是否被单击过:

var clicked = false
document.getElementById('button').addEventListener("click", function() {
   clicked = true
}?);?

addEventListener on MDN

在 MDN 上添加事件监听器

回答by Ashad Ahmad

All the answers here discuss about onclick method, however you can also use addEventListener().

这里的所有答案都讨论了 onclick 方法,但是您也可以使用 addEventListener()。

Syntax of addEventListener()

addEventListener() 的语法

document.getElementById('button').addEventListener("click",{function defination});

The function defination above is known as anonymous function.

上面的函数定义称为匿名函数。

If you don't want to use anonymous functions you can also use function refrence.

如果不想使用匿名函数,也可以使用函数引用。

function functionName(){
//function defination
}



document.getElementById('button').addEventListener("click",functionName);

You can check the detail differences between onclick() and addEventListener() in this answer here.

您可以在此处的答案中查看onclick() 和 addEventListener() 之间的详细差异。

回答by Ganesh Ktheitroadala

if(button.clicked==true){
console.log("Button Clicked);
} ==> // This Code Doesn't Work Properly So Please Use Below One // 

var button= document.querySelector("button"); // Accessing The Button // 
button.addEventListener("click", check); // Adding event to call function when clicked // 

function check(){ 
console.log("clicked");
}; // This Code Works Fine //