jQuery/js-根据状态和数量选择选项计算税款、小计和总额
jQuery / js - Calculate tax, subtotal and total based on state and quantity select options
我是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 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>
我在这里看到的一个大问题是,您的iftax
变量是在您作为参数传递给$('state').change();
的匿名函数的范围内声明的
您必须将其声明为全局变量,而不是在所述函数中重新声明:
var ifTaxRate = 0; //new
$(document).ready(function() {
$("#state").change(function() {
if ($(this).val() == 'ID') {
ifTaxRate = .06;
} else {
ifTaxRate = 0;
}
doMath();//new
});
//this way every time you change a select box value, doMath() is called
$('select').change(doMath);
});
这样,只要你需要,它就可以访问…
更新
至于没有显示在div中的内容,不要使用
$('.quantityselected').value = quant;
它不起作用有两个不同的原因:第一:.value = ...
(jQuery中的.val(...)
)是本机javascript,不能在jQuery对象中工作
第二:value是input和select控件的属性,使用div必须设置.innerText
(在jQuery中为.text()
)和.innerHTML
(在jQuery中为.html()
)
用途:
$('.quantityselected').html(quant);
...
您的问题可能是范围之一。
由于您在状态更改函数的闭包中声明变量iftax
,因此它在doMath
方法中不可见。
在内部,您应该在顶层声明变量,然后仅从状态更改函数向其分配:
<script type="text/javascript">
var iftax;
$(document).ready(function() {
$("#state").change(function() {
if ($(this).val() == 'ID') {
iftax = .06;
} else {
iftax = 0;
}
});
});
// rest as before
(在一个完全不同的主题上,调用变量ifTaxRate
或类似的变量IMHO会更清楚,因为目前我认为它可能与到期税额混淆,尤其是在与price
和shipping
相同的上下文中使用时。)
在文档就绪函数中声明的变量"iftax"是该函数的本地变量。doMath函数中引用的iftax将查看全局范围(window.iftax),我猜这是未定义的。
尽管使用全局变量有点反模式,但如果您从文档就绪函数中的所有位置删除"var"(它当前被写为"var iftax"),它将默认为全局变量,您的代码应该可以工作。
- 计算用户从多个文件中选择的选项数量
- CKEditor中没有从计算机上传图像的选项
- JS函数,用于计算产品数量和选项附加税
- 如何计算量角器中下拉选项的总数
- 以N/a作为选项计算评级
- 从选择选项下拉列表中计算多个值
- 使用Selenium网络驱动程序计算ng repeat生成的选项卡集中的选项卡数
- Javascript 根据选中的选项计算值
- 计算从打开选项卡到关闭选项卡的时间
- 如何使用 jquery 计算选项卡的高度以向下推页脚
- Shopify (液体) 计算选择和更改样式中的选项大小
- jQuery - 如何计算 if/else 语句的两个选择选项值
- jQuery/js-根据状态和数量选择选项计算税款、小计和总额
- 使用jquery根据所选选项计算小计和总价
- 根据选择的选项显示一个Div(带有计算值)——Angular
- 使用javascript与选择选项进行计算
- 计算selectize.js中的选项个数
- 用Javascript和/或jQuery计算select选项的值
- Javascript实时价格计算(从选项和复选框)
- 选择带开关箱的选项计算