简体   繁体   English

我无法在按钮的第二次单击上使用第二个功能

[英]I can’t get the second function to work on the second click of the button

I can't get the second function to work. 我无法使用第二个功能。 Here is the code that I need help with: HTML: 这是我需要帮助的代码:HTML:

<fieldset>
  <legend>Store</legend>
  <p id="p"></p>
  Spaceship: $2900
  <input type="button" value="Buy" onClick="buy()" />
</fieldset>

JS: JS:

function buy() {
    var isStart = false;
    var clickCount = 0;
    if (!isStart) {
        clickCount++;
        if (clickCount == 1) {
            var money = 10000;
            money = money - 2900;
            $("#p").text(money);
        }
        if (clickCount == 2) {
            var money = 10000;
            money = money - 2900 * 2;
            $("#p").text(money);
        }
    }
}

Please help me! 请帮我!

your clickCount and isStart values are being set back to 0 and false whenever you click the button. 您的clickCountisStart值将分别设置为0false无论何时单击该按钮。 put both values outside of the function: 将两个值都放在函数之外:

    var isStart = false;
    var clickCount = 0;
    var money=10000;
    function buy() {
        if  (!isStart) {
            clickCount++;
            if (clickCount == 1) {
                money=money-2900;
                $("#p").text(money);
            }
            if (clickCount == 2) {
                money=money-2900;
                $("#p").text(money);
            }
        }
    }

EDIT 编辑

Like @Sebastien says in the last part of his answer, you're also redeclaring money as well, so be sure to only declare that once, outside of the function. 就像@Sebastien在回答的最后一部分中所说的那样,您也要重新声明money ,因此请确保仅在函数外部声明一次。

You redeclare clickCount every time you execute the function and therefore reset it to false... 您每次执行函数时都重新声明clickCount ,因此将其重置为false。

To maintain a "global" state for clickCount , you must define it outside the function that uses it. 要维持clickCount的“全局”状态,您必须在使用它的函数之外定义它。

 var clickCount = 0; function buy() { var isStart = false; if (!isStart) { clickCount++; if (clickCount == 1) { console.log('clickCount == 1'); var money = 10000; money = money-2900; $("#p").text(money); } if (clickCount == 2) { console.log('clickCount == 2'); var money = 10000; // Reset here, so the result is the same money = money-2900; $("#p").text(money); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset> <legend>Store</legend> <p id="p"></p> Spaceship: $2900 <input type="button" value="Buy" onClick="buy()" /> </fieldset>var clickCount = 0; function buy() { var isStart = false; if (!isStart) { clickCount++; if (clickCount == 1) { console.log('clickCount == 1'); var money = 10000; money = money-2900; $("#p").text(money); } if (clickCount == 2) { console.log('clickCount == 2'); var money = 10000; // Reset here, so the result is the same money = money-2900; $("#p").text(money); } } } 

Also note that you do the same thing with the money variable so the final amount is always the same. 另请注意,您对money变量执行相同的操作,因此最终金额始终相同。

You must declare var money = 10000; 您必须声明var money = 10000; outside the function. 功能之外。 Inside the function you only change the amount like: money = money-2900; 在函数内部,您只需要更改金额,例如: money = money-2900; .

UPDATE 更新

Here is how you would basically do it. 这是您基本上要执行的操作。 All you need to do is keep track of the money in a global variable. 您需要做的就是在全局变量中跟踪资金。

Now, on every click you will subtract the cost of the product from the remaining money. 现在,每次点击都会从剩余的钱中扣除产品的成本。

See how many variables and checks were unnecessary ;) 看看有多少变量和检查是不必要的;)

 var money = 10000; function buy() { money = money-2900; $("#p").text(money); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset> <legend>Store</legend> <p id="p"></p> Spaceship: $2900 <input type="button" value="Buy" onClick="buy()" /> </fieldset> 

Now, you will notice that you can still buy the product even when you don't have enough money. 现在,您会发现,即使您没有足够的钱,仍然可以购买该产品。 I leave it to you to solve that problem. 我把它留给你解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM