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

Why does #39;valueAsNumber#39; return NaN as a value?(为什么“valueAsNumber返回 NaN 作为值?)
本文介绍了为什么“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 作为值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)