我是 js/jQuery 的新手。原谅这个可怕的剧本。我迷路了。
*(按照建议使用全局变量 ifTaxRate 更新。)*
我正在尝试根据客户选择的状态和订购数量计算税金、小计和总计,并在屏幕上动态显示。
如果客户来自爱达荷州,我将尝试应用 6% 的销售税率。
选择选项:
<select id="state" value="<?php echo $_SESSION['sstate'] ?>" name="state" class="required" >
<option value="">Select State</option>
<option value="AK">Alaska</option>
.
<option value="ID">Idaho</option>
.
.
<option value="WY">Wyoming</option>
</select>
<select id="orderquantity" name="orderquantity">
<option value="1">1 Bottle (30 Day Supply)</option>
.
.
<option value="8">8 Bottles (240 Day Supply)</option>
</select>
显示信息的 div:
<div class="quantityselected"></div>
<div class="productprice"></div>
<div class="pricequantity"></div>
<div class="subtotal"></div>
<div class="tax"></div>
<div class="shipping"></div>
<div class="total"></div>
真正糟糕的 js 尝试:
<script type="text/javascript">
var ifTaxRate;
$(document).ready(function() {
$("#state").change(function() {
if ($(this).val() == 'ID') {
ifTaxRate = .06;
} else {
ifTaxRate = 0;
}
});
});
function doMath() {
var quant = parseInt(document.getElementById('orderquantity').value);
//change price here
var price = 69.99;
var tax = ifTaxRate;
//change flat shipping cost here
var shipping = 4.99;
var subtotal = quant * price;
var taxtotal = tax * subtotal;
var total = subtotal + tax;
$('.quantityselected').value = quant;
$('.productprice').value = price;
$('.pricequantity').value = subtotal;
$('.tax').value = taxtotal;
$('.shipping').value = shipping;
$('.total').value = shipping;
}
</script>