JavaScript - 计算按钮被按下的次数(函数)

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

JavaScript - Count how many time a button is pressed(function)

javascripthtmlbuttononclick

提问by GDesigns

I'm trying to count how many times a button is pressed, however I dont think this is right, because the count button keeps incrementing even if the 'Saying Hello' button isn't clicked

我正在尝试计算按钮被按下的次数,但是我认为这是不对的,因为即使没有点击“打招呼”按钮,计数按钮也会不断增加

Any thoughts?

有什么想法吗?

<!DOCTYPE html>

<html>
    <head>
        <title>JSExample</title>
    </head>

    <body>

        <script type="text/javascript">
            function hello() {
               alert("Hello there!");
            }

            var countClicks = 0;

            function upCount() { 

                if(document.getElementById('hello').onclick = "hello()") {
                    countClicks++;
                    alert(countClicks);
                    return true;
                } else {
                    return false;
                }

            }

        </script>

        <p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>

        <p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>

    </body>

</html>

采纳答案by egellon

Y, i don't exactly understand your problem. Right now on click 'Saying hello' you are calling a function 'hello()' which only pops up an alert with 'Hello there'. On clicking 'Counting' button, you are checking a very strange condition that i don't exactly understand and then increment the value. So, if you want to count how many times 'say hello' was clicked, and then present it with 'Counting' button, use this code.

是的,我不太明白你的问题。现在点击“说你好”,你正在调用一个函数“hello()”,它只会弹出一个带有“你好”的警报。单击“计数”按钮时,您正在检查一个我不完全理解的非常奇怪的条件,然后增加该值。因此,如果您想计算点击“say hello”的次数,然后使用“Counting”按钮呈现它,请使用此代码。

<!DOCTYPE html>

 <html>
        <head>
            <title>JSExample</title>
        </head>
    
        <body>
    
            <script type="text/javascript">
                function hello() {
                   alert("Hello there!");
          countClicks++;
                }
    
                var countClicks = 0;
    
                function upCount() { 
        alert(countClicks);
                }
    
            </script>
    
            <p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>
    
            <p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>
    
        </body>
    
    </html>

If this is not what you meant, please let me know :)

如果这不是你的意思,请告诉我:)

回答by veproza

The condition if(document.getElementById('hello').onclick = "hello()") {is not correct. I'm not sure what it should be doing, but know what it does: it assigns a string hello()to the .onclickcallback of the helloelement. As a Stringcan't be executed (as opposed to a Function), it effectively removes the callback. You probably want something like this

条件if(document.getElementById('hello').onclick = "hello()") {不正确。我不确定它应该做什么,但知道它做什么:它为元素hello().onclick回调分配一个字符串hello。由于 aString无法执行(相对于 a Function),它有效地删除了回调。你可能想要这样的东西

var countClicks = 0;

function hello() {
   alert("Hello there!");
   countClicks++;
}

function upCount() { 
    alert(countClicks);
}
<p><input type="button" name="button" value="Saying Hello" id="hello" onclick="hello();"/></p>
<p><input type="button" name="click" value="Counting" id="click" onclick="upCount();"/></p>

回答by Kancho Iliev

if(document.getElementById('hello').onclick == "hello()")

you've missed one =

你错过了一个=