让时间自动进入输入类型="时间"

时间:2015-01-26 08:53:49

标签: javascript jquery css html5

我正在尝试在加载页面时将当前时间显示为输入中的值。但是,由于某种原因它不起作用。有什么建议?谢谢你的帮助。

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <script src="Scripts/Global.js"></script>
      <script src="Scripts/jquery.js"></script>   
      <link rel="stylesheet" type="text/css" href="Styles/Design.css">
   </head>
   <body>
      <input type="time" id="theTime">
      <script>
         $(document).ready( function() {
          var current = new Date();
          var minute = (now.getMinutes() + 1);               
          var seconds = now.getSeconds();
          if(minute < 10) 
              minute = "0" + minute;
          if(seconds < 10) 
              seconds = "0" + seconds;
          var today = now.getFullYear() + '-' + minute + '-' + seconds;
          $('#theTime').val(today);
         });
      </script>
   </body>
</html>

3 个答案:

答案 0 :(得分:3)

使用

var now = new Date();

而不是

 var current = new Date();

答案 1 :(得分:1)

您正在使用错误的变量current,而不是

$(document).ready( function() {
          var current = new Date();
          var minute = (current.getMinutes() + 1);               
          var seconds = current.getSeconds();
          if(minute < 10) 
              minute = "0" + minute;
          if(seconds < 10) 
              seconds = "0" + seconds;
          var today = current.getFullYear() + '-' + minute + '-' + seconds;
          $('#theTime').val(today);
         });

查看工作代码here

答案 2 :(得分:1)

使用valueAsDate使输入显示值

  <body>
      <input type="time" id="theTime">
      <script>
          $(document).ready( function() {
              var now = new Date();
              //now2 prevents the milliseconds from showing up in the input
              var now2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
              $('#theTime')[0].valueAsDate = now2;
          });
      </script>
  </body>