想從以下的代碼以總計的形式,添加所有items
的price
。在javascript中添加數組的項目
document.getElementById('form-signin').addEventListener('submit', saveItem);
function saveItem(e){
var items= document.getElementById('items').value;
var date= document.getElementById('date').value;
var price= document.getElementById('price').value;
var expense= {
items: items,
date: date,
price: price
}
if(localStorage.getItem('bill')==null){
var bill=[];
bill.push(expense);
localStorage.setItem('bill', JSON.stringify(bill));
} else{
var bill = JSON.parse(localStorage.getItem('bill'));
bill.push(expense);
localStorage.setItem('bill', JSON.stringify(bill));
console.log(bill);
}
e.preventDefault();
}
function fetchResult(){
var bill = JSON.parse(localStorage.getItem('bill'));
var result = document.getElementById('total');
result.innerHTML='';
for(var i=0;i < bill.length;i++){
var items= bill[i].items;
var date= bill[i].date;
var price= bill[i].price;
result.innerHTML+= '<table class="table table-bordered">' + '<tr>'+
'<th>'+'Items'+'</th>'+
'<th>'+'Date'+'</th>'+
'<th>'+'Price'+'</th>'+
'</tr>'+ '<tr>'+
'<td>'+ items + '</td>'+
'<td>'+ date+ '</td>'+
'<td>'+ price + '</td>'+
'</tr>'+ '</table>';
}
This is output of aboce. Now how to calculate total ??
因此循環所有項目並添加價格? – epascarello