JavaScript新手。
我正在尝试创建一个简单的计算器,其显示的值会随着用户输入更多信息而动态更新。我的问题:绿色圆圈中显示的“速率”始终显示为“NaN”,直到用户输入最终的表格值(所有值)。
我尝试使用.value,parseFloat等。我会感激任何建议或更正 - 我知道JS不是很优雅(也不是很好)。
JSFiddle: http://jsfiddle.net/thinkhuman/8ftuw/
谢谢!
更新:海报解决了下面的NaN问题,但问题的另一半是:当“associated_costs”时,“rate”值(在绿色cirlce中)不会更新和不可计费的价值变化。关于那部分的任何建议?
<!--Web page for the JS app-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>What's My Rate?</title>
<!-- <link href='http://fonts.googleapis.com/css?family=Nunito:400,300' rel='stylesheet' type='text/css'>
--> <link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="title">
<h1>What's My Rate?</h1>
<h3>Calculate your real-world freelance rate for web design/development.</h3>
</div>
<div class="calcwrapper">
<div class="entries">
<label for="targetsalary">Target salary (yearly):</label>
<input type="text" id="target_salary" name="targetsalary" onchange="calculate();">
<br />
<label for="associatedcosts">Associated costs (%):</label>
<input type="text" id="associated_costs" name="associatedcosts" onchange="calculate();">
<br />
<label for="timeoff">Time off (hours/year):</label>
<input type="text" id="timeoff" name="timeoff" onchange="calculate();">
<br />
<label for="nonbillable">Non-billable time (%):</label>
<input type="text" id="nonbillable" name="nonbillable" onchange="calculate();">
<br />
<label for="profit">Desired profit (%):</label>
<input type="text" id="profit" name="profit" onchange="calculate();">
</div>
<div class="ratedisplay">
<p class="rate" id="required_rate"></p>
<p class="ratesubtitle">per hour</p>
</div>
<div class="moreinfo">
<p>For a detailed explanation of why calculating your real-world rate is critical, see <a href="http://blog.teamtreehouse.com/calculate-hourly-freelance-rates-web-design-development-work" target="_blank"> Neil Tortorella's excellent blog post </a> on Teamtreehouse.com.</p>
</div>
</div>
<script src="whatsmyrate.js"></script>
</body>
</html>
CSS:
body {
/* font-family: 'Nunito', sans-serif;
*/ color: #384047;
font-family: Arial,Helvetica, sans-serif;
}
.calcwrapper{
max-width: 700px;
height: 250px;
margin: 10px auto;
padding: 10px 10px;
background: #f4f7f8;
border-radius: 8px;
border: 2px solid black;
}
.entries{
max-width: 400px;
padding: 10px 20px;
float:left;
}
.ratedisplay{
background-color: #5fcf80;
float: right;
color: #fff;
height: 150px;
width: 150px;
font-size: 4.0em;
border: 1px solid #3ac162;
margin-right: 150px;
margin-bottom:30px;
line-height: 30px;
text-align: center;
border-radius: 100%;
box-shadow: 0 -5px 0 rgba(255,255,255,0.1) inset;
}
.ratesubtitle{
font-size: 0.4em;
color: black;
margin-top: 10px;
}
.rate{
color: #FFF;
text-align: center;
margin-bottom: 10px;
vertical-align:middle;
text-shadow: 0 3px 0 rgba(255,255,255,0.2);
}
.title{
margin: auto;
text-align: center;
}
input{
display: inline-block;
border: none;
margin-bottom: 5px;
margin-left: 20px;
max-width: 100px;
font-size: 18px;
height: auto;
/* margin: 0;
*/ outline: 0;
background-color: #e8eeef;
color: #8a97a0;
box-shadow: 0 2px 0 rgba(0,0,0,0.02) inset;
}
.moreinfo{
display:inline-block;
margin: auto;
text-align: center;
}
label{
display: inline-block;
float: left;
width: 200px;
text-align: right;}
... aaa和JavaScript:
//A tool for calculating your real-world hourly rate for web design/development work.
/***************
PROCESS OVERVIEW
****************/
//1. Specify target_salary.
//2. Specify associated_costs %.
//3. Calculate new target_salary (target_salary + associated_costs).
//4. Specify time_off (holidays, vacation, sick days), and subtract from total_yearly_hours.
//5. Specify non-billable time, subtract from total hours.
//6. Calculate overhead % ((hourly_rate * overhead)+ hourly_rate)
//7. Specify profit %, and add to hourly_rate
/*********
VARIABLES
**********/
//target_salary
//associated_costs(%)
//total_required_salary = target_salary + associated_costs
//time_off = 176 hours (holidays 56, vacation 80 , sick days 40)
//non_billable_time = 25%
//overhead_costs(%)
//profit
//total_yearly_hours = 2080
//billable_hours = total_yearly_hours - (time_off + non_billable_time)
//required_rate = total_required_salary / billable_hours
//document.getElementById("required_rate").style.display='none';
function calculate() {
var total_yearly_hours = 2080;
// Get values for ALL elements on screen
var target_salary = document.getElementById("target_salary");
var associated_costs = document.getElementById("associated_costs");
var timeoff = document.getElementById("timeoff");
var nonbillable = document.getElementById("nonbillable");
var profit = document.getElementById("profit");
var required_rate = document.getElementById("required_rate");
// Get the user's input from the input elements.
var salary = parseFloat(target_salary.value);
var costs = parseFloat(associated_costs.value) / 100;
var vacation = parseFloat(timeoff.value);
var freehours = parseFloat(nonbillable.value);
var extra = parseFloat(profit.value) / 100;
// Compute the total required salary and billable hours.
var total_salary = (salary * (costs / 100)) + salary;
var billable_hours = total_yearly_hours - ((freehours/100)+vacation);
var rate = "$" + Math.floor(((total_salary * extra) + total_salary) / billable_hours);
//rate = total salary + profit, / billable hours.
// If the result is a finite number, the user's input was good
if (isNaN(rate)) {
required_rate.innerHTML = rate;
}
else{
required_rate.innerHTML = "";
}
}
答案 0 :(得分:5)
在你的情况下,你正在做一个字符串$NaN
,正如你所做的那样
var rate = "$" + Math.floor(((total_sala ...
看看你如何添加moneyign使它成为一个字符串,那不是NaN,它是一个字符串
删除美元符号
var rate = Math.floor(((total_sala ...
然后做(注意你的代码中的条件是错误的)
if (isNaN(rate)) {
required_rate.innerHTML = "";
} else {
required_rate.innerHTML = '$' + rate;
}
比较条件中的实际数字,然后添加美元符号