【发布时间】:2013-07-04 12:09:22
【问题描述】:
如何使当前时间显示在警报框中?我是编程新手,所以我完全迷路了。我正在用 html5 或 javascript 编码。我在这里添加了大部分代码。
这是我的代码:
<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>
我做错了吗?
【问题讨论】:
-
document.getElementById('txt').innerHTML 而不是 'txt'.value。 innerHTML 可用于设置和获取值。
-
'txt'.value //what do I do here?- 我们也想知道。为什么不直接将return来自startTime的字符串调用,然后将结果放入警报中? -
看看@momentjs.com
-
提供的代码不完整
-
@user2542058 脚本不应位于
<body>和<html>标签之外。相反,您应该将<html>标记之外的脚本附加到<body>标记
标签: javascript html time