作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于某种原因,我的代码只是不想计算。我正在练习一本练习书,但在这方面它并没有真正帮助我。对于总和,我得到类似 20000NaN 的东西,我知道这不是一个数字。我怎样才能让它正确计算?我尝试使用 ParseFloat 来转换我的百分比。谢谢。
<form action="#" name="car">
<p> </p>
<p>Vehicle:
<select id="dropDown1">
<option selected value="370z, 20000, 10%, 8.5%">Accord</option>
<option value="M3, 18000, 8%, 8.5%">Altima</option>
<option value="Camaro, 19000, 9%, 8.5%">Camry</option>
</select>
<br />
<br />
</p>
<button type="button" onclick="car()" value="Submit">Book it now</button>
<br />
<br />
<textarea name="answer" cols="80" rows="3" disabled></textarea>
<br />
<br />
</form>
</div>
<script language="javaScript">
function car() {
var value = document.getElementById("dropDown1").value;
var split = value.split(",");
var v1 = split[0];
var v2 = split[1];
var v3 = split[2];
var v4 = split[3];
parseFloat(v3);
parseFloat(v4);
profit = v2*v3;
tax = v2*v4;
extra = profit+tax;
sum = v2 + extra;
document.car.answer.value = "You selected " + v1 + " " + "which has a base price of" + v2 + ", a profit of" + v3 + ", and sales tax of" + v4 + "." + " Total cost of this vehicle, including profit and sales tax is: $" + sum + "."
}
</script>
最佳答案
您的大部分语法都不正确。事情应该是这样的:
var test = function() {
var value = document.getElementById("dropDown1").value;
var split = value.split(",");
var v1 = split[0];
var v2 = split[1];
var v3 = split[2];
var v4 = split[3];
v3 = parseFloat(v3);
v4 = parseFloat(v4);
var profit = parseFloat(v2)*parseFloat(v3)/100;
var tax = parseFloat(v2)*parseFloat(v4)/100;
var extra = parseFloat(profit)+parseFloat(tax);
var sum = parseFloat(v2) + parseFloat(extra);
document.getElementById("answer").value = "You selected " + v1 + " " + "which has a base price of $" + v2 + ", a profit of " + v3 + "%, and sales tax of " + v4 + "%. Total cost of this vehicle, including profit and sales tax is: $" + sum + ".";
};
<form action="#" name="car">
<p> </p>
<p>Vehicle:
<select id="dropDown1">
<option selected value="370z, 20000, 10%, 8.5%">Accord</option>
<option value="M3, 18000, 8%, 8.5%">Altima</option>
<option value="Camaro, 19000, 9%, 8.5%">Camry</option>
</select>
<br />
<br />
</p>
<button type="button" onclick="test()" value="Submit">Book it now</button>
<br />
<br />
<textarea id="answer" cols="80" rows="3" disabled></textarea>
<br />
<br />
</form>
关于javascript - 取钱有困难吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39989077/
我是一名优秀的程序员,十分优秀!