无法在Javascript中使用toFixed方法
我试图学习Javascript并创建一些简单的脚本,我试图为自己创建一个运费脚本。计算到达物品的运输成本。除了数字格式之外,一切都正常。我正在尝试使用无法在Javascript中使用toFixed方法,javascript,html,Javascript,Html,我试图学习Javascript并创建一些简单的脚本,我试图为自己创建一个运费脚本。计算到达物品的运输成本。除了数字格式之外,一切都正常。我正在尝试使用toFixed()方法格式化数字。但它不起作用。我检查了控制台结果是 Uncaught TypeError: undefined is not a function (index):23 calculate (index):23 onclick 这是我的index.php文件: <html> <head>
toFixed()
方法格式化数字。但它不起作用。我检查了控制台结果是
Uncaught TypeError: undefined is not a function (index):23
calculate (index):23
onclick
这是我的index.php文件:
<html>
<head>
<title>Shipping cost</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="wrapper">
Price : <input type="number" name="price" id="price" />
Pounds : <input type="number" name="pounds" id="pounds" />
<input type="button" value="Calculate" onclick="calculate()" />
</div>
<script type="text/javascript">
function calculate(){
var price = document.getElementById('price').value;
var pounds = document.getElementById('pounds').value;
var rule = parseFloat(price) * 0.04 + parseFloat(pounds) * 7;
var total = price + rule;
var result = total.toFixed(2);
document.write(result);
}
</script>
</body>
</html>
运费
价格:
英镑:
函数计算(){
var price=document.getElementById('price').value;
var pounds=document.getElementById('pounds')。值;
var规则=parseFloat(价格)*0.04+parseFloat(英镑)*7;
var总额=价格+规则;
var结果=总固定值(2);
记录(结果);
}
price
是一个字符串,因此price+rule
也是一个字符串。而且字符串没有固定的toFixed
方法
如果你想要数字,你可以使用
price
是一个字符串,因此price+rule
也是一个字符串。而且字符串没有固定的toFixed
方法
如果你想要数字,你可以使用
price
是一个字符串,因此price+rule
也是一个字符串。而且字符串没有固定的toFixed
方法
如果你想要数字,你可以使用
price
是一个字符串,因此price+rule
也是一个字符串。而且字符串没有固定的toFixed
方法
如果你想要数字,你可以使用
不要使用
文档。请编写,请参阅中的警告。改用DOM方法。不要使用文档。编写,请参阅中的警告。改用DOM方法。不要使用文档。编写,请参阅中的警告。改用DOM方法。不要使用文档。编写,请参阅中的警告。改用DOM方法。
var price = +document.getElementById('price').value,
pounds = +document.getElementById('pounds').value,
rule = price * 0.04 + pounds * 7,
total = price + rule,
result = total.toFixed(2);