简体   繁体   English

如何在此条件语句中获取按钮点击数据?

[英]How do I get a button clicked data in this conditional statement?

I'm making a rock paper scissors game right now.我现在正在制作剪刀石头布游戏。 I'm trying to compare the users input and the computer's automatically generated answer in an if statement, but I can't figure out how to find the user's input.我试图在 if 语句中比较用户输入和计算机自动生成的答案,但我不知道如何找到用户的输入。

Edit: Here's the rest of my code.编辑:这是我的代码的 rest。 I've changed a few things from my compare function. Still nothing works.我已经从我的比较 function 中更改了一些东西。仍然没有任何效果。 But I hope that by adding my HTML and JavaScript you'd be able to figure out what is going on.但我希望通过添加我的 HTML 和 JavaScript,您能够弄清楚发生了什么。

Here's my code:这是我的代码:

function showInstruct() {
            var show = document.getElementById('hideInstruct');
            if (show.style.display === "none") {
                show.style.display = "block";
            }
            else {
                show.style.display = "none"
            }
        }

        function hideButtons() {
            document.getElementById('buttonDiv').style.display = 'none';
            document.getElementById('countdown').style.display = 'flex';
            document.getElementById('nextRound').style.display = 'none';

            revealRps();

            setCountdown();
        }

        function setCountdown() {
            var numberS = 3;
            var x = setInterval(function() {
            document.getElementById("number").innerHTML = numberS;
            numberS--;
            if (numberS < 0) {
                clearInterval(x);
                document.getElementById("number").innerHTML = "VS";
                document.getElementById('rps').style.display = 'none';
                document.getElementById('nextRound').style.display = 'flex';
                document.getElementById('input').style.display = 'none';
                compare();
            }
            }, 950);
        }

        function revealRps() {
            document.getElementById('rps').style.display = 'flex';
            document.getElementById('nextRound').style.display = 'none';
        }

        function hideRps() {
            document.getElementById('rps').style.display = 'none';
            document.getElementById('input').style.display = 'flex';
        }

        function compare() {
            var random = ['rock', 'paper', 'scissors'];
            var comp = Math.floor(Math.random() * random.length);
            var compDraw = random[comp];
            if($('rock').click == true && compDraw == 'scissors')
            {
            document.getElementById('rockBeats').style.display = 'flex';
            trackedValue.rock = true
            }
            else if ($('paper').click == true && compDraw == 'rock')
            {
                document.getElementById('paperBeats').style.display = 'flex';
                trackedValue.paper = true
            }
            else if ($('scissors').click == true && compDraw == 'paper')
            {
                document.getElementById('scissorsBeats').style.display = 'flex';
                trackedValue.scissors = true
            }
            else if ($('rock').click == true && compDraw == 'paper')
            {
                document.getElementById('paperBeats').style.display = 'flex';
                trackedValue.paper = true
            }
            else if ($('paper').click == true && compDraw == 'scissors')
            {
                document.getElementById('scissorsBeats').style.display = 'flex';
                trackedValue.scissors = true
            }
            else if ($('scissors').click == true && compDraw == 'rock') 
            {
                document.getElementById('rockBeats').style.display = 'flex';
                trackedValue.rock = true
            }

        }

And my HTML:还有我的 HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Extreme Rock Paper Scissors!</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0l">
    <link href="https://fonts.googleapis.com/css2?family=Indie+Flower&family=Open+Sans&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Righteous&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=New+Rocker&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=New+Rocker&family=Schoolbell&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Long+Cang&family=New+Rocker&family=Schoolbell&display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="RPS.css">
    <script type="text/javascript" src="RPS.js"></script>

    <div class="titleWords">
        <h1>EXTREME ROCK PAPER SCISSORS!</h1>
    </div>

</head>
<body>

    <div id="beats">
        <div id="rockBeats">ROCK BEATS SCISSORS</div>
        <div id="paperBeats">PAPER BEATS ROCK</div>
        <div id="scissorsBeats">SCISSORS BEATS PAPER</div>
        <div id="draw">DRAW</div>
        <div id="noInput">YOU DIDN'T CLICK ANYTHING. COMPUTER AUTOMATICALLY WINS</div>
    </div>
    <div id="buttonDiv">
        <button id="playButton" = onclick="hideButtons()">PLAY GAME</button>
        <button id="instructions" onclick="showInstruct()">INSTRUCTIONS<button>

    </div>


    <div id="hideInstruct">
        <p>The premise - This is war! And you must choose your attacks carefully.
        When you press PLAY GAME a countdown clock will countdown from 3. During that time you need to make a selection of how you're going to attack. Are you going to use Rock, Paper, or Scissors? 
        </p>
        <br>
        <p>The logistics - If you choose Rock and the computer chooses scissors, you crush those scissors into dust!  If you choose scissors and the computer chooses paper, you slice the paper into shreds! And if you choose paper and the computer chooses rock, you smear the rock with the a major paper-cut! However, if you choose Rock and the computer chooses Paper than you lose. In essense, the computer can do the same things to you that you can do to it. If you and the computer both draw the same thing, than it's considered a draw and nothing happens. And one more thing. If time runs out and you've selected nothing, than the computer automatically wins the point.</p>
        <br>
        <p>How to win - For each victory, that's one point. The first player to reach 3 points wins the game!</p>
    </div>

    <article id="countdown">
        <p id="number">3</p>
    </article>

    <footer id="rps">
        <button type="button" id="rock" onclick="hideRps()">ROCK</button>
        <button type="button" id="paper" onclick="hideRps()">PAPER</button>
        <button type="button" id="scissors" onclick="hideRps()">SCISSORS</button>
    </footer>

    <div id="input">
        <p id="locked">Your answer is locked in</p>
    </div>

    <div id="nextRound">
        <button id="nextButton" onclick="hideButtons()">Next Round</button>
    </div>

</body>
</html>

From my understanding of your ques, you can use an object to keep track of the value根据我对你的问题的理解,你可以使用 object 来跟踪值

let trackedValue = {rock:false,paper:false,sicssors:false };

        function compare() {
        var random = ['rock', 'paper', 'scissors'];
        var comp = Math.floor(Math.random() * random.length);
        var compDraw = random[comp];
        if($('rock').click == true && compDraw == 'scissors')
        {
            document.getElementById('rockBeats').style.display = 'flex';
            trackedValue.rock = true
        }
        // remaining code

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

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