我正在为学校建立一个项目,并且很难让toFixed
工作。我收到一条错误消息:
toFixed不是函数
我已取出toFixed
以获取正确的值。尝试输入多个项目,列表将构建,但具有两个以上的小数位。
$(document).ready(function(){
$.ajax({
url: "https://galvanize-eats-api.herokuapp.com/menu",
method: "GET",
dataType: "json"
}).done(function(data){
var x = data["menu"];
for (var i = 0; i < x.length; i++) {
var y = x[i]["name"]
var z = x[i]["price"]
$(".menu").append('<option class="food" value='+z+'>'+y+''+z+'</option>')
}
})
})
$("#addItem").click(function(){
var arr = [];
var total = 0;
var item = $(":selected",".menu").text();
var quantity = $('#quan').val();
$("select :selected").each(function(){
arr.push($(this).val());
});
for (var i = 0; i < arr.length; i++) {
total += (arr[i]* quantity);
}
$('#totals').append('<tr><td>'+total+'</td>');
});
$("#addItem").click(function(){
colSum();
colTax();
});
function colSum() {
var sum=0;
//iterate through each input and add to sum
$('tbody td').each(function() {
sum += parseFloat($(this).text());
});
//change value of total
$('#mySum').html(sum);
$('tbody td').each(function() {
sum += parseFloat($(this).text()*.07);
});
//change value of total
$('.grand').html(sum);
}
function colTax(){
var sum=0;
//iterate through each input and add to sum
$('tbody td').each(function() {
sum += parseFloat($(this).text()*.07);
});
//change value of total
$('.tax').html(sum);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="pages/order.css" media="screen" title="no title" charset="utf-8">
<title>Eats</title>
</head>
<body>
<header>
<h1>Eats</h1>
</header>
<div class="left">
<select class="menu" multiple="names"></select>
<form class="order" action="index.html" method="post">
<input id="quan" type="number" name="name" value="">
<button id="addItem" type="button" name="button">Add Item</button><br>
<input id="name" type="text" name="name" value=""><br>
<input id="phone" type="text" name="phone" value=""><br>
<input id="address"type="text" name="address" value=""><br>
<button type="button" name="button">Deliver It</button>
</form>
</div>
<div class="right">
<div class="totalsS">
<table id="totals">
</table>
</div>
<p>subtotal</p> <div class="subtotal">
<dl>
<dd id="mySum"></dd>
</dl>
</div>
<p>tax</p><div class="tax"></div>
<p>total</p><div class="grand"></div>
</div>
<footer>txt</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="pages/order.js"></script>
</body>
</html>