简体   繁体   中英

I am trying to create a buy system using a button

I am trying to create a system, which, displays the amount of money you have, displays the cost of what you are purchasing, has a button which, when clicked takes away an amount of your money, and increases the amount it takes away each click, however, the button just doesn't work.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="buyA.js">
    </script>
    <title>Test Page</title>
</head>
<body>
    <p id="money">
        <script>
        document.getElementById("money").innerHTML = "Money: " + money;
        </script>
    </p>
    <p id="cost">
        <script>
        document.getElementById("cost").innerHTML = "Cost: " + buyA;
        </script>
    </p>
    <input id="Purchase" type="button" value="Purchase" onclick="BuyA();"/>
</body>
</html>

var money = 1000;
var buyA = 500;

function BuyA() {
    if (money >= buyA) {
        money - buyA;
        buyA + 250;
    }
    else {
        window.alert("Not Enough Money")
    }
}

You're putting your scripts in the wrong place. Plus you're making mistakes on assigning values ( money and buyA ).

 <!DOCTYPE html> <html> <head> <script type="text/javascript" src="buyA.js"> </script> <title>Test Page</title> <!-- Put your scripts here --> <script> var money = 1000; var buyA = 500; function BuyA() { if (money >= buyA) { // deduct the money // money - buyA; money = money - buyA; // increase the price // buyA + 250; buyA = buyA + 250; // update the values document.getElementById("money").innerHTML = "Money: " + money; document.getElementById("cost").innerHTML = "Cost: " + buyA; } else { window.alert("Not Enough Money") } } </script> </head> <body> <p id="money"></p> <p id="cost"></p> <script> // update the values document.getElementById("money").innerHTML = "Money: " + money; document.getElementById("cost").innerHTML = "Cost: " + buyA; </script> <input id="Purchase" type="button" value="Purchase" onclick="BuyA();"/> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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