0

私の仕事は、カリフォルニア州の居住者が購入したときに消費税を請求することだけが必要です. 私は、顧客が州のドロップダウン リストからカリフォルニア州を選択したときに消費税を適用する JavaScript 関数になることを期待したものを書きました。各州には「else」の値を、カリフォルニアには「CA」の値を付けました。私のコードは消費税を毎回 0 として計算するため、「CA」が選択されている場合、総計に税金が適用されません。状態リストには onSelect=UpdateCost() を使用し、製品のチェック ボックスがオンの場合は onChange=UpdateCost() を使用しています。コードのどの部分が税の適切な計算を妨げていますか?

 function UpdateCost() {
   var stotal = 0;
   var ttotal = 0;
   var gtotal = 0;
   var tax = .0825;
   var gn, elem;
     for (i=0; i<12; i++) {
       gn = 'manual'+i;
       elem = document.getElementById(gn);
     if (elem.checked == true) { stotal += Number(elem.value); }
}
   var state = document.getElementById('state');
   var selection = state.options[state.selectedIndex].value;
 if (selection = 'CA') { ttotal += tax * stotal; }
 if (selection = 'else') {ttotal = 0;}
 if(!isNaN(ttotal)) {
    var calitax = ttotal.toFixed(2);
    document.getElementById('taxtotal').value = calitax;
    document.getElementById('subtotal').value = stotal.toFixed(2);
    }
   var gtotal = ttotal + stotal;
     if(!isNaN(gtotal)) {
   var grand = gtotal.toFixed(2);
   document.getElementById('grandtotal').value = grand;}

   } 

そして、これは単純化された関連するフォームコードです (とにかく簡単に取得できます):

//in the header of course.
<script type="text/javascript" src="orderform.js"></script>

<form action=""  method="post">
<fieldset>
State: <select name="state" id="state" onSelect="UpdateCost()">
<option value="else" id="else">AL</option>
<option value="else" id="else">AK</option>
<option value="else" id="else">AZ</option>
<option value="else" id="else">AR</option>
<option value="CA" id="CA">CA</option>
</select>
<p>Select the manuals to order</p>

<input name="manual" type="checkbox" id='manual6' onclick="UpdateCost()" value="185">manual 1<br />
<input name="manual" type="checkbox" id='manual0' onclick="UpdateCost()" value="220">manual 2<br /><br />

<input name="manual" type="checkbox" id='manual7' onclick="UpdateCost()" value="155">manual 3<br />
<input name="manual" type="checkbox" id='manual1' onclick="UpdateCost()" value="185">manual 4<br /><br />

<h3>**SAVE $50** WHEN YOU BUY BOTH</h3><br />
<input name="manual" type="checkbox" id='manual8' onclick="UpdateCost()" value="290">manual 5<br />
Or:<br />
<input name="manual" type="checkbox" id='manual2' onclick="UpdateCost()" value="355">manual 6<br /><br />
<hr>
<input name="manual" type="checkbox" id='manual9' onclick="UpdateCost()" value="190">manual 7<br />
<input name="manual" type="checkbox" id='manual3' onclick="UpdateCost()" value="225">manual 8<br /><br />

<input name="manual" type="checkbox" id='manual10' onclick="UpdateCost()" value="125">manual 9<br />
<input name="manual" type="checkbox" id='manual4' onclick="UpdateCost()" value="150">manual 10<br /><br />
<h3>**SAVE $50** WHEN YOU BUY BOTH</h3><br />
<input name="manual" type="checkbox" id='manual11' onclick="UpdateCost()" value="265">11<br />
Or:<br />
<input name="manual" type="checkbox" id='manual5' onclick="UpdateCost()" value="325">12<br /><br />

Subtotal: <input name="subtotal" type="text" id="subtotal" value=""><br />
California Resident Tax: <input name="taxtotal" type="text" id="taxtotal" value=""><br />
Total: <input name="grandtotal" type="text" id="grandtotal" value=""><br />

</fieldset>

長々とすみません!!!助けてくれてありがとう:D

4

1 に答える 1

0

あなたには2つの問題がありました。最初は、状態選択フィールドのonSelect代わりに使用していました。onChangeしかし、より大きな問題は、状態をチェックしていた関数にありました。=比較の代わりに代入を使用していました==

if (selection == 'CA') {    
    ttotal += tax * stotal;
}
if (selection == 'else') {
    ttotal = 0;
}

jsFiddle の例を修正しました。

于 2013-04-10T20:53:00.270 に答える