我遇到了尝试调试的奇怪错误。我有一个输入字段,其中将当前时间预先显示为该值。当我运行页面时,我收到引用Javascript文件第8行的Uncaught Type错误(document.getElemntByID(“ theTime”)。valueAsDate = b;)

是否试图说未定义ValueAsDate?我要用什么代替它?任何帮助深表感谢。

html

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8" content="HTML,CSS,XML,JavaScript">
       <script src="Scripts/jquery.js" type="text/javascript"></script>
       <script src="Scripts/Global.js" type="text/javascript"></script>


      <link rel="stylesheet" type="text/css" href="Styles/Design.css">
   </head>
         <body>
               <form action="" method="post">
                  <fieldset>
                     <table border="0px" >
                        <tr>
                           <td colspan = "2" id="formHeader">
                              Select Date and Time
                           </td>
                        </tr>
                        <tr>
                           <td class="spaceUnder"  id="formBody">Date: &nbsp;&nbsp;<input type="date" id="theDate"></td>
                           <td  align="center" class="spaceUnder"  id="formBody">Time: &nbsp;<input type="time" id="theTime" ></td>
                        </tr>
                     </table>
                  </fieldset>
               </form>
         </body>
</html>


的JavaScript

$(document).ready(function() {
    myTimer();
});
function myTimer() {
    var a = new Date();
    var b = new Date(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours() - 8, a.getMinutes());
    //$("#theTime")[0].valueAsDate = b;
    document.getElementById("theTime").valueAsDate =b;
}

最佳答案

它的类型是时间,而不是日期

<input type="time" id="theTime">


document.getElemnetById("theTime)”是TIME

var b = new Date(a.getFullYear(), a.getMonth(), a.getDate(), a.getHours() - 8, a.getMinutes());


b是DATE。

试试这个

b.setTime(document.getElementById("theTime"));
b.setHours(b.getHours() - 8);

09-12 08:39