# 我的Javascript 購物車 算total?

<script language="JavaScript">

function tally()

{

Cost = 0;

if (document.orderform.Item1.value) { Cost = Cost + Item1.value *26.15; }//26.15是單價

if (document.orderform.Item2.value) { Cost = Cost + Item2.value *26.10; }//26.10是單價

if (document.orderform.Item3.value) { Cost = Cost + Item3.value *26; }//26是單價

Cost = dollar(Cost);

document.orderform.Total.value = "\$" + Cost;

}

</script>

<form method="post" name="orderform" action="mailto:me@mydomain.com" enctype="text/plain"">

<table border="0">

<tr><td colspan="4">

<p>item 1<input type="text" name="Item1" value="0" > <!--輸入數量-->

<p>item 2<input type="text" name="Item2" value="0" > <!--輸入數量-->

<p>item 3<input type="text" name="Item3" value="0" > <!--輸入數量-->

</td></tr>

<tr>

<td> Total <input type="text" name="Total" value="\$0" size="7"></td>

</tr>

<tr><td colspan="2" align="center"><input type="Submit" value="Send Order"></td>

</tr>

</table>

</form>

Update:

Rating

＜script language="JavaScript"＞

function tally()

{ fm = document.orderform

Cost = fm.Item1.value *26.15 +

fm.Item2.value *26.10; +

fm.Item3.value *26;

fm.Total.value = "\$" + Cost;

}

＜/script＞

2010-05-25 20:47:00 補充：

可是沒看見有觸發 function tally() 呀，請版大查清楚。

onsubmit = "tally()"

寫在 submit tag，或form tag 內。

有問題可寄信來。

2010-05-27 20:04:35 補充：

item1, item2, item3 輸入"1" 後會顯示 "01" ，因此在 onchange 加進小指令使它變成 "1" 。

在 function 內設 fm 代表 orderform 物件，使以後省點功夫。

<html>

<script type="text/javascript">

function tally()

{ fm = document.orderform

Cost = fm.Item1.value *26.15 +

fm.Item2.value *26.10 +

fm.Item3.value *26

fm.Total.value = "\$" + Cost

}

</script>

<body>

<form method="post" name="orderform" action="mailto:me@mydomain.com" enctype="text/plain">

<table border="0">

<tr><td colspan="4">

<p>item 1<input type="text" name="Item1" value="0" onchange="this.value=this.value*1; tally()">

<p>item 2<input type="text" name="Item2" value="0" onchange="this.value=this.value*1; tally()">

<p>item 3<input type="text" name="Item3" value="0" onchange="this.value=this.value*1; tally()">

</td></tr>

<tr>

<td> Total <input type="text" name="Total" value="\$0" size="7"></td>

</tr>

<tr><td colspan="2" align="center"><input type="Submit" value="Send Order"></td>

</tr>

</table>

</form>

</body>

</html>

function tally()

{

document.orderform.Total.value ="\$"+ document.orderform.Item1.value *26.15

+document.orderform.Item2.value *26.10

+document.orderform.Item3.value *26;

}

<p>item 1<input type="text" name="Item1" value="0" onChange="tally()">

<p>item 2<input type="text" name="Item2" value="0" onChange="tally()">

<p>item 3<input type="text" name="Item3" value="0" onChange="tally()">