<i id='drpKb'><tr id='drpKb'><dt id='drpKb'><q id='drpKb'><span id='drpKb'><b id='drpKb'><form id='drpKb'><ins id='drpKb'></ins><ul id='drpKb'></ul><sub id='drpKb'></sub></form><legend id='drpKb'></legend><bdo id='drpKb'><pre id='drpKb'><center id='drpKb'></center></pre></bdo></b><th id='drpKb'></th></span></q></dt></tr></i><div id='drpKb'><tfoot id='drpKb'></tfoot><dl id='drpKb'><fieldset id='drpKb'></fieldset></dl></div>

      <small id='drpKb'></small><noframes id='drpKb'>

      <tfoot id='drpKb'></tfoot>

      • <bdo id='drpKb'></bdo><ul id='drpKb'></ul>
      <legend id='drpKb'><style id='drpKb'><dir id='drpKb'><q id='drpKb'></q></dir></style></legend>

        为什么“valueAsNumber"返回 NaN 作为值?

        时间:2023-08-07
        • <legend id='aEZc2'><style id='aEZc2'><dir id='aEZc2'><q id='aEZc2'></q></dir></style></legend>
            <tbody id='aEZc2'></tbody>
          <tfoot id='aEZc2'></tfoot>

        • <i id='aEZc2'><tr id='aEZc2'><dt id='aEZc2'><q id='aEZc2'><span id='aEZc2'><b id='aEZc2'><form id='aEZc2'><ins id='aEZc2'></ins><ul id='aEZc2'></ul><sub id='aEZc2'></sub></form><legend id='aEZc2'></legend><bdo id='aEZc2'><pre id='aEZc2'><center id='aEZc2'></center></pre></bdo></b><th id='aEZc2'></th></span></q></dt></tr></i><div id='aEZc2'><tfoot id='aEZc2'></tfoot><dl id='aEZc2'><fieldset id='aEZc2'></fieldset></dl></div>
            <bdo id='aEZc2'></bdo><ul id='aEZc2'></ul>

              <small id='aEZc2'></small><noframes id='aEZc2'>

                  本文介绍了为什么“valueAsNumber"返回 NaN 作为值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在下面的代码中,我试图调用 valueAsNumber 但我只得到一个 NaN 返回.当我使用 parseInt 时,我得到了我期望的结果.这是为什么呢?

                  In the code below, I am trying to call valueAsNumber but I just get a NaN returned. When I use parseInt I get the result I expect. Why Is this?

                  <html>                                                                                                                                                                                                     
                  <head>                                                                                                                                                                                                     
                  <title>JavaScript: Demo 1</title>                                                                                                                                                                          
                  <link rel="stylesheet" type="text/css" href="index.css">                                                                                                                                                   
                  </head>                                                                                                                                                                                                    
                  
                  <body>                                                                                                                                                                                                     
                  <div id="numbers">                                                                                                                                                                                         
                    <div id="inputs">                                                                                                                                                                                        
                      <form name="inputForm">                                                                                                                                                                                
                      <div class="prompt">Number 1: <input name="number1" type="text"></div>                                                                                                                                 
                      <div class="prompt">Number 2: <input name="number2" type="text"></div>                                                                                                                                 
                      </form>                                                                                                                                                                                                
                    </div>                                                                                                                                                                                                   
                    <div id="result">                                                                                                                                                                                        
                      <div class="prompt">RESULT: <span id="operation_result">&nbsp;</span></div>                                                                                                                            
                    </div>                                                                                                                                                                                                   
                  </div>                                                                                                                                                                                                     
                  <div id="operations">                                                                                                                                                                                      
                  <p><a id="add_link" href="#" onClick="add(this)">ADD</a></p>                                                                                                                                               
                  </div>                                                                                                                                                                                                     
                  
                  <script type="text/javascript">                                                                                                                                                                            
                    function add(linkElement){                                                                                                                                                                               
                    // var value1 = parseInt(document.inputForm.number1.value);
                    // var value2 = parseInt(document.inputForm.number2.value);
                  
                    var value1 = document.inputForm.number1.valueAsNumber;
                    var value2 = document.inputForm.number2.valueAsNumber;                                                                                                                                              
                      var result = value1 + value1;                                                                                                                                                                          
                  
                      document.getElementById('operation_result').innerHTML = result;                                                                                                                                        
                    }                                                                                                                                                                                                        
                  </script>                                                                                                                                                                                                  
                  
                  </body>                                                                                                                                                                                                    
                  </html>                                                                                                                                                                                                    
                  

                  推荐答案

                  考虑到属性名称,您的期望是合理的,但阅读实际 规格/文档:

                  Your expectations are reasonable considering the property name, but reading actual specs/documentation:

                  valueAsNumber IDL 属性表示元素的值,解释为数字.

                  The valueAsNumber IDL attribute represents the value of the element, interpreted as a number.

                  获取时,如果 valueAsNumber 属性不适用,如定义对于输入元素的类型属性的当前状态,然后返回一个非数字 (NaN) 值.

                  On getting, if the valueAsNumber attribute does not apply, as defined for the input element's type attribute's current state, then return a Not-a-Number (NaN) value.

                  这是一个表格,它列出了适用于 valueAsNumbertype.它们是:

                  Here's a table that list's types that apply to valueAsNumber. These are:

                  • 日期和时间 (datetime)(请注意,此 type="" 现在在 HTML LS 中已过时)
                  • 日期(日期)
                  • 月()
                  • 周()
                  • 时间(时间)
                  • 本地日期和时间 (datetime-local)
                  • 编号(编号)
                  • 范围(范围)
                  • Date and Time (datetime) (Note this type="" is now obsolete in HTML LS)
                  • Date (date)
                  • Month (month)
                  • Week (week)
                  • Time (time)
                  • Local Date and Time (datetime-local)
                  • Number (number)
                  • Range (range)

                  观察到 type="text" 明显不在上述列表中,因此 textInput.valueAsNumber 将始终返回 NaN 即使 isNaN( parseInt( textInput.value, 10 ) ) === false.

                  Observe that type="text" is conspicuously absent from the above list, therefore textInput.valueAsNumber will always return NaN even when isNaN( parseInt( textInput.value, 10 ) ) === false.

                  这篇关于为什么“valueAsNumber"返回 NaN 作为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JavaScript 类型转换:(true &amp;&amp; 1) vs (true 下一篇:将 io.BytesIO 转换为 io.StringIO 以解析 HTML 页面

                  相关文章

                  最新文章

                  <legend id='ToVZN'><style id='ToVZN'><dir id='ToVZN'><q id='ToVZN'></q></dir></style></legend>

                  <small id='ToVZN'></small><noframes id='ToVZN'>

                  • <bdo id='ToVZN'></bdo><ul id='ToVZN'></ul>
                  <tfoot id='ToVZN'></tfoot>
                    <i id='ToVZN'><tr id='ToVZN'><dt id='ToVZN'><q id='ToVZN'><span id='ToVZN'><b id='ToVZN'><form id='ToVZN'><ins id='ToVZN'></ins><ul id='ToVZN'></ul><sub id='ToVZN'></sub></form><legend id='ToVZN'></legend><bdo id='ToVZN'><pre id='ToVZN'><center id='ToVZN'></center></pre></bdo></b><th id='ToVZN'></th></span></q></dt></tr></i><div id='ToVZN'><tfoot id='ToVZN'></tfoot><dl id='ToVZN'><fieldset id='ToVZN'></fieldset></dl></div>