简体   繁体   中英

How to make the current time appear in an alert box

How does one make the current time appear in an alert box? I'm new to programming, so I'm completely lost. I'm coding in html5 or javascript. I've added most of my code here.

Here's my code:

<script>
    function startTime(){
        var today=Date();
        var h=today.getHours();
        var m=today.getMinutes();
        var s=today.getSeconds(); 
        document.getElementById('txt').innerHTML=h+":"+m+":"+s; 
    }

</script>
<div id="txt"></div>

<h1>What would you like it to say?</h1>

    <p>Commmon Requests:</p>

    <form action="">
    <select name="requests" onchange ="checkIfOther();" id="dropDown1">
    <option value="blank"> </option>
    <option value="good morning sir">Good Morning Sir</option>
    <option value="current time">Current Time</option>
    <option value="other">Other</option>    
    </select>
    </form>

    </p>
<button onclick="mySubmit()" value>Submit</button>

    <div id="other" style="display:none">
        <br><br><label>Optional Request: </label><input type="text" id="otherText"/>
    </div>

</body>
</html>

<script>
    function checkIfOther(){
        a=document.getElementById("dropDown1");        
        if(a.value == "other"){           
            document.getElementById("other").setAttribute("style","display:inline");
        }
        else{
            document.getElementById("other").setAttribute("style","display:none");
            }
    }   
</script>

<script type="text/javascript">
    function mySubmit(){
        var x=document.getElementById("dropDown1");
        if(x.value == "other"){
            alert("You have chosen: " + otherText.value); 
        }
        else if(x.value == "current time"){
            alert("The current time is: " + 'txt'.value); //what do I do here?
        }
        else{   
            alert("You have chosen: " + x.options[x.selectedIndex].text);
        }   
    }   
</script>

Am I doing something wrong?

First, your HTML is completely screwed. Validate it.

As for the code itself, startTime is broken. It needs new Date() .

function startTime(){
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds(); 
  return [ h, m, s ].join(':')
}

and to get it into an alert box you can just do:

alert(startTime());

If you want to put it in an element like <div id="txt"></div> you can do:

document.getElementById('txt').innerHTML = startTime();

You can do something like this.

...
else if(x.value == "current time"){
    startTime();
    alert("The current time is: " + document.getElementById('txt').innerHTML);
...

Make a function getTime that returns the current time string:

function getTime(){
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds(); 
    return h+":"+m+":"+s; 
}

Then use that:

…
else if(x.value == "current time"){
    alert("The current time is: " + getTime());
}
…

If you still need the startTime function, you can use it there as well:

function startTime() {
    document.getElementById('txt').innerHTML = getTime();
}

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