parseFloat()返回NaN

时间:2016-05-08 22:08:23

标签: javascript html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript" src="jquery.min.js"></script>  

  </head>
  <body>
      
    <style>
        .form-control {
            margin-bottom: 15px;
        }
    </style>
      
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">
            <h2 style="margin: 0px; padding: 0px;">Billing Calculator</h2>
          </a>
        </div>
      </div>
   </nav>
      
      <div class="col-md-2">
          <form class="navbar-form navbar-left" role="search">
              <div class="form-group">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">
                  <input type="date" class="form-control date" placeholder="Search">                  
              </div>
        </form>
     </div>
      
     <div class="col-md-3">
         <form class="navbar-form navbar-left" role="search">
             <div class="form-group">
                  <input type="text" class="form-control" id="one" placeholder="hours">
                  <input type="text" class="form-control" id="two" placeholder="hours">
                  <input type="text" class="form-control" id="three" placeholder="hours">
                  <input type="text" class="form-control" id="four" placeholder="hours">
                  <input type="text" class="form-control" id="five" placeholder="hours">
                  <input type="text" class="form-control" id="six" placeholder="hours">
                  <input type="text" class="form-control" id="seven" placeholder="hours">
                  <button type="submit" class="btn btn-default" id="click">Calculate</button>
             </div>
         </form>     
     </div>
     <div class="col-md-1">
        <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
                <label for="form-control">Total Hours:</label>
                <label for="form-control" id="totalHours">.</label>
            </div> 
        </form> 
     </div>  
     <script type="text/javascript">
         
        var hoursOne = parseFloat(document.getElementById("one").value); 
         
        document.getElementById("click").onclick = function(){
            alert(hoursOne);    
        }
     </script>  
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

我正在尝试从文本输入中检索一个数字,即使我使用NaN,它也始终会提醒innerHTML。该计划是一个计费计算器,它接收工作小时数并返回总小时数。日期与值之间并没有太大关系,但可以帮助用户跟踪。

2 个答案:

答案 0 :(得分:1)

int permits = theUnsafe.getInt(object, offset);
if (!enough(permits))
    permits = theUnsafe.getVolatileInt(object, offset);

在填写表单之前,您的代码会被执行。您需要获取函数内的数据。

答案 1 :(得分:0)

将混乱减少到重要的地位,你有:

<form class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input type="text" class="form-control" id="one" placeholder="hours">
    <button type="submit" class="btn btn-default" id="click">Calculate</button>
  </div>
</form>
<script type="text/javascript">
  var hoursOne = parseFloat(document.getElementById("one").value); 
  document.getElementById("click").onclick = function(){
    alert(hoursOne);    
  }
</script>

因此,您在页面加载时设置 hoursOne 的值,而不是将其重置为输入的当前值。因此值为“”(空字符串),parseFloat("") NaN 。您需要做的是在单击按钮时获取值。

另外,给控件一个名称以便它可以成功,并给一个默认值,这样如果用法出错就只返回NaN:

document.getElementById("click").onclick = function(){
  alert(parseFloat(parseFloat(this.form.hoursOne.value)));    
}
<form>
  <label for="one">Hours: 
    <input type="text" class="form-control" id="one" name="hoursOne" value="0">
  </label>
  <button type="button" id="click">parseFloat</button>
</form>

最后,您不应该使用占位符而不是使用带有所需格式提示的正确标签。如果您使用占位符作为标签,一旦用户输入了某些内容,他们将无法再看到该标签,也不知道输入的内容。