jQuery/js-根据状态和数量选择选项计算税款、小计和总额

jQuery / js - Calculate tax, subtotal and total based on state and quantity select options

本文关键字:计算 选项 选择 js- 状态 jQuery      更新时间:2023-09-26

我是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会更清楚,因为目前我认为它可能与到期税额混淆,尤其是在与priceshipping相同的上下文中使用时。)

在文档就绪函数中声明的变量"iftax"是该函数的本地变量。doMath函数中引用的iftax将查看全局范围(window.iftax),我猜这是未定义的。

尽管使用全局变量有点反模式,但如果您从文档就绪函数中的所有位置删除"var"(它当前被写为"var iftax"),它将默认为全局变量,您的代码应该可以工作。