Javascript 尝试在单击按钮时输入时间戳

Javascript 尝试在单击按钮时输入时间戳,javascript,html,button,input,timestamp,Javascript,Html,Button,Input,Timestamp,这是我正在编写的代码,我无法理解如何让按钮只输入当前时间的基本非计数时间戳。有人能帮我解决我的问题吗。我所要做的就是在“获取时间”按钮旁边的框中放置一个时间戳 <html> <head> <script language="JavaScript" type="text/javascript"> function getTimeStamp() { var now = new Date(); return ((now.getMon

这是我正在编写的代码,我无法理解如何让按钮只输入当前时间的基本非计数时间戳。有人能帮我解决我的问题吗。我所要做的就是在“获取时间”按钮旁边的框中放置一个时间戳

<html>
<head>
<script language="JavaScript" type="text/javascript">



function getTimeStamp() {
       var now = new Date();
       return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
                     + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
                     .getSeconds()) : (now.getSeconds())));
}


window.onclick = "getTimeStamp" ;


</script>

</head>

<body>
<td>
<button type="button" onclick="form"><form name="getTimeStamp">

 <input type=text" name="field" value="" size="11">
</form>Get Time</button></td>




<td>Test</td>
</tr>
</body>
</html>

函数getTimeStamp(){
var now=新日期();
return((now.getMonth()+1)+'/'+(now.getDate())+'/'+now.getFullYear()+“”+now.getHours()+“”):'
+((now.getMinutes()<10)?(“0”+now.getMinutes()):(now.getMinutes())+:“+((now.getSeconds()<10)?(“0”+now
.getSeconds():(现在是.getSeconds());
}
window.onclick=“getTimeStamp”;

您不能将表单放入按钮中,按钮必须位于表单中。您需要将返回的值写入可以看到的位置

<form>
  <button type="button" onclick="this.form.timeField.value=getTimeStamp()">Get time stamp</button>
  <input type="text" name="timeField" size="11">
</form>
它将字符串“getTimeStamp”分配给window的onclick属性,并且没有做任何有用的事情

您还可以删除:

language="JavaScript" type="text/javascript"

很久以前,只有在非常特殊的情况下才需要第一个,而第二个除了在HTML4中需要之外,从来没有真正需要过。不再需要它了。:-)

代码中有一些基本错误

这是一个工作示例:

<html>
<head>
<script type="text/javascript">
function getTimeStamp() {
       var now = new Date();
       return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
                     + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
                     .getSeconds()) : (now.getSeconds())));
}
function setTime() {
    document.getElementById('field').value = getTimeStamp();
}
</script>

</head>

<body onload="setTime()">

<input id="field" type="text" name="field" value="" size="11" />
<button type="button" onclick="setTime();">Get Time</button>

</body>
</html>

函数getTimeStamp(){
var now=新日期();
return((now.getMonth()+1)+'/'+(now.getDate())+'/'+now.getFullYear()+“”+now.getHours()+“”):'
+((now.getMinutes()<10)?(“0”+now.getMinutes()):(now.getMinutes())+:“+((now.getSeconds()<10)?(“0”+now
.getSeconds():(现在是.getSeconds());
}
函数setTime(){
document.getElementById('field')。value=getTimeStamp();
}
有时间
  • 不能在
    按钮下嵌套
    表单
    ;在这种情况下,您可以跳过
    表单
  • 您需要以某种方式确定要设置时间的
    输入
  • 您可以通过将其设置为ID来访问此
    输入
  • 在我的示例中,我使用
    body
    元素中的
    onload
    事件来设置初始时间戳
  • 如果你有任何问题可以问他们

    <html>
    <head>
    <script type="text/javascript">
    function getTimeStamp() {
           var now = new Date();
           return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
                         + ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
                         .getSeconds()) : (now.getSeconds())));
    }
    function setTime() {
        document.getElementById('field').value = getTimeStamp();
    }
    </script>
    
    </head>
    
    <body onload="setTime()">
    
    <input id="field" type="text" name="field" value="" size="11" />
    <button type="button" onclick="setTime();">Get Time</button>
    
    </body>
    </html>