简体   繁体   中英

second button can only be clicked if first button has been clicked

So I have this script for a clock where when you click on start the current time gets displayed. the same happens when stop is clicked. What I want to happen now is that when I click on the first button that the second button gets activated as in you can only click the second button when the first one is clicked.

My HTML

**<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="style/new-style.css">
  <title>Digital Clock</title>
</head>

<body>
<center>
  <button class="panel2" onclick="CurrentTime()">Start rit</button>
  <p>Start van de Rit: <span id="Start Rit" /></p>
  <button class="panel1" onclick="ZaWarduu()">Stop rit</button>
  <p>Einde van de Rit: <span id="Einde Rit"/></p>
  <p>Clock : <span id="Clock" /></p>
    <script type="text/javascript">
    </script>
  </center>
  </body>

  </html>

Javascript code

    //Met deze functie word de tijd op dit moment laten zien in de html code met het id:clock
    var time;
    function ItsShowTime(){
            var date = new Date();
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();

            h = (h < 10) ? "0" + h : h;
            m = (m < 10) ? "0" + m : m;
            s = (s < 10) ? "0" + s : s;
            time = h + ":" + m + ":" + s;

            document.getElementById("Clock").textContent = time;
            setTimeout(ItsShowTime, 1000);
    }

    //Met deze functie zorg je ervoor dat als je klikt op start rit de tijd op dit moment er komt te staan.
    function CurrentTime(){
      //var value kijkt hoeveel er word geklikt op de knop start rit. Je kan dat zijn door de pagina te inspecteren en naar console te gaan als je daar eenmaal bent klik op start rit dan zie je hoe de var value omhoog gaat bij een laat hij de tijd zjin bij twee komt de alert te staan.
      var value = parseInt(document.getElementById('Start Rit').value, 10);
      value = isNaN(value) ? 0 : value;
      value++;
      document.getElementById('Start Rit').value = value;
      console.log(value);
      // deze if statement zorgt ervoor dat als er nog een keer geklikt word op start dat er een alert oppupt.
      if (value == 1) {
        document.getElementById("Start Rit").innerText = time;

      }
      if(value != 1){
        alert("Rit kan niet opnieuw gestart worden.");
      }
    }
    function ZaWarduu(){
      var value2 = parseInt(document.getElementById('Einde Rit').value, 10);
      value2 = isNaN(value2) ? 0 : value2;
      value2++;
      document.getElementById('Einde Rit').value = value2;
      console.log(value2);
      // deze if statement zorgt ervoor dat als er nog een keer geklikt word op start dat er een alert oppupt.
      if (value2 == 1) {
        document.getElementById("Einde Rit").innerText = time;

      }
      if(value2 != 1){
        alert("Rit kan niet opnieuw gestopt worden.");
      }
    }

    function KeepTime(){
      if (Location.reload(DigitalClock.html)){

      }
    }
    ItsShowTime();
    KeepTime();

I looked at a couple of similar questions on stack overflow and tried to look at some search result in google unfortunatly I couldn't find anything.

例如,通过 localStorage 在这两个按钮之间共享状态,如果您想增加复杂性,也可以通过像 Redux 这样的商店共享状态 :)

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