jQuery计算错误的两个值并不确定原因

时间:2016-03-19 03:19:42

标签: javascript jquery

为什么我试图一起计算的总数为1.00?



$(document).ready(function() {
  // Hide initial values that need updating
  $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
  // get current delivery rate
  $("#get_rate").trigger('click');
  // set a timeout and get total and shipping that was generated and add together for nnew total
  setTimeout(function() {
    // get cart sub-total
    var a = $(".cart-total span").html().replace(/[$£]/gi, "");
    var ib = $("#estimated-shipping em").html().replace(/[$£]/gi, "");

    if (ib == "FREE") {
      $("#estimated-shipping em").html("FREE");
      var b = "0.00";
    } else {
      var b = parseFloat($("#estimated-shipping em").text().replace(/\$|£/gi, ""));
    }

    // add together sub-total and estimated shipping to new total
    // update with new total with sub-total and added shipping
    var total = parseFloat(a) + parseFloat(b);
    total = parseFloat(Math.round(total * 100) / 100).toFixed(2);
    $('.cart-finalTotal span').text("£" + total);

    // show new values
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
  }, 2000);
  $(".item-quantity input").on("change", function() {
    document.location.href = location.href
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="cart-right">


  <p class="cart-total">Sub-Total<span class="money">£1,245.00</span>
  </p>
  <p class="cart-vat">VAT 20% (included)<span class="money" style="display: block;">£249.00</span>
  </p>
  <p class="cart-delivery">Delivery (estimated)<span class="money" id="estimated-shipping">+ <em style="display: inline;">FREE</em></span>
  </p>
  <p class="cart-finalTotal">Total<span class="money" style="display: block;">£0.00</span>
  </p>

</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在你的小计中,你有一个逗号,当parseFloat看到一个不是一个点的非数字字符时它会停止。所以parse float返回1而不是1245.你需要你的正则表达式更多的是/ [^ 0-9。] / g。

以下代码正在运行:

$(document).ready(function() {
  // Hide initial values that need updating
  $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").hide();
  // get current delivery rate
  $("#get_rate").trigger('click');
  // set a timeout and get total and shipping that was generated and add together for nnew total
  setTimeout(function() {
    // get cart sub-total
    var a = $(".cart-total span").html().replace(/[^0-9.]/gi, "");

    if ($("#estimated-shipping em").text() == "FREE") {
      $("#estimated-shipping em").html("FREE");
      var b = "0.00";
    } else {
      var b = parseFloat($("#estimated-shipping em").text().replace(/[^0-9.]/gi, ""));
    }

    // add together sub-total and estimated shipping to new total
    // update with new total with sub-total and added shipping
    var total = parseFloat(a) + parseFloat(b);
    total = parseFloat(Math.round(total * 100) / 100).toFixed(2);
    $('.cart-finalTotal span').text("£" + total);

    // show new values
    $("#estimated-shipping em, .cart-finalTotal span, .cart-vat span").show();
  }, 2000);
  $(".item-quantity input").on("change", function() {
    document.location.href = location.href
  });
});