Html
<input type="button" value="總計(expend)" onclick="calculate('expend')">
<p id="SumValue_expend"></p>
<input type="button" value="總計(deposit)" onclick="calculate('deposit')">
<p id="SumValue_deposit"></p>
<input type="button" value="總計(total)" onclick="calculate('total')">
<p id="SumValue_total"></p>
Javascript
function calculate(classSelect)
{
var money_list={};
money_list.name="收支明細表";
money_list.time="2020/10";
money_list.list=[
{name:"薪水",price:45000,time:"10/5",class:"deposit"},
{name:"房租",price:7500,time:"10/10",class:"expend"},
{name:"朋友聚餐",price:890,time:"10/13",class:"expend"},
{name:"水電費",price:780,time:"10/15",class:"expend"},
{name:"周年慶(化妝品)",price:15700,time:"10/17",class:"expend"},
{name:"中秋獎金",price:300,time:"10/25",class:"deposit"}
];
//
tempTotal=0;
if (classSelect=='total')
{
for (i=0; i<money_list.list.length; i++)
{
tempTotal = tempTotal + money_list.list[i].price;
}
}
else
{
for (i=0; i<money_list.list.length; i++)
{
if (money_list.list[i].class==classSelect)
{
tempTotal = tempTotal + money_list.list[i].price;
}
}
}
//
switch (classSelect) {
case 'expend':
document.getElementById("SumValue_expend").innerHTML='金額(expend) : '+tempTotal;
break;
case 'deposit':
document.getElementById("SumValue_deposit").innerHTML='金額(deposit) : '+tempTotal;
break;
case 'total':
document.getElementById("SumValue_total").innerHTML='金額(total) : '+tempTotal;
break;
}
}