简体   繁体   English

未捕获的类型错误:无法将属性'valueAsDate'设置为null

[英]Uncaught Type Error: Cannont set Property 'valueAsDate' of null

I am getting this weird error that I attempting to de-bug. 我遇到了尝试调试的奇怪错误。 I have an input field that has the current time pre displayed as the value. 我有一个输入字段,其中将当前时间预先显示为值。 when I run the page i get the Uncaught Type error referring to the Javascript file line number 8 (document.getElemntByID("theTime").valueAsDate=b;) 当我运行页面时,我收到引用Javascript文件第8行的Uncaught Type错误(document.getElemntByID(“ theTime”)。valueAsDate = b;)

Is it trying to say ValueAsDate is not defined? 是否试图说未定义ValueAsDate? What do I put in place of it? 我要用什么代替它? Any help is most appreciated. 非常感谢您的帮助。

html 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 的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;
}

Its type is time, not date 它的类型是时间,而不是日期

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

" document.getElemnetById("theTime) " is TIME document.getElemnetById("theTime) ”是TIME

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

b is DATE . b是DATE

Try something like this, 试试这个

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

The question should have been how to set input type date and input type time with current values in javascript. 问题应该是如何使用javascript中的当前值设置输入类型日期和输入类型时间。

And this just so that any one who searches this question in future may get help. 这样一来,以后再搜索此问题的任何人都可以得到帮助。 Because I did not find any answers on SO related to this. 因为我没有找到与此相关的任何答案。

Input type date expects date in this format yyyy-mm-dd. 输入类型date需要采用 yyyy-mm-dd 格式的日期。

Input time type expects time in this format hh:rr:ss format. 输入时间类型期望使用以下格式的时间hh:rr:ss格式。

So the following function should do the task. 因此,以下功能应完成任务。

       function myTimer() {
         var dateObj = new Date();
         document.getElementById('theDate').value = dateObj.toISOString().slice(0,10);
         document.getElementById('theTime').value = dateObj.toTimeString().slice(0,8);
       }

But these features are not yet fully supported in all browsers. 但是,并非所有浏览器都完全支持这些功能。

I tested it in chrome and safari and it is working fine. 我在chrome和safari中测试了它,效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误-无法读取null的属性“ owlcarousel” - Uncaught Type Error - Cannot read property 'owlcarousel' of null 未被捕获的TypeError:无法设置null WordPress错误的属性&#39;isNotDirty&#39; - Uncaught TypeError: Cannot set property 'isNotDirty' of null WordPress error WordPress jQuery错误:“未捕获的TypeError:无法将属性&#39;value&#39;设置为null” - WordPress jQuery error: “Uncaught TypeError: Cannot set property 'value' of null” JavaScript 错误:未捕获的类型错误:无法设置 null 的属性“值” - JavaScript Error: Uncaught TypeError: Cannot set property 'value' of null Javascript 控制台错误未捕获的类型错误:无法将属性“onloadedmetadata”设置为 null - Javascript console error Uncaught TypeError: Cannot set property 'onloadedmetadata' of null 未捕获的类型错误:无法设置 null 的属性“onmouseover” - Uncaught TypeError: Cannot set property 'onmouseover' of null 未捕获的TypeError:无法将属性&#39;onclick&#39;设置为null - Uncaught TypeError: Cannot set property 'onclick' of null 未捕获的类型错误:无法设置 null 的属性“宽度” - Uncaught TypeError: Cannot set property 'width' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;href&#39;设置为null - Uncaught TypeError: Cannot set property 'href' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM