gpt4 book ai didi

javascript - parseFloat() 返回 NaN

转载 作者:行者123 更新时间:2023-11-28 01:12:22 25 4
gpt4 key购买 nike

<!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>

我正在尝试从文本输入中检索数字,即使我使用 innerHTML,它也总是提示 NaN。该程序旨在成为一个计费计算器,计算工作小时数并返回总小时数。日期实际上与值没有太大关系,但可以帮助用户跟踪。

最佳答案

 <script type="text/javascript">             
document.getElementById("click").onclick = function(){
var hoursOne = parseFloat(document.getElementById("one").value);
alert(hoursOne);
}
</script>

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

关于javascript - parseFloat() 返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com