【问题标题】:After click the select option update product price shows wrong in opencart单击选择选项更新产品价格后在opencart中显示错误
【发布时间】:2016-06-03 11:57:49
【问题描述】:

当我点击选择的选项产品价格更新显示错误。 这是我的代码

<div class="container">
      <div class="row">
      <div class="col-md-6">Initial Price: <span id="thisIsOriginal" class="">$45,000.00</span></div>
      <div class="col-md-6">Total: <span id="total">$45,000.00</span></div>
      </div>
      <div class="row">
        <select class="optionPrice" name="select-1">
          <option value="">Please Select</option>
          <option data-price="2,000.00" value="20">+$2,000.00</option>
        </select>
      </div>
    </div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $('.optionPrice').change(function () {
        var OriginalPrice = $('#thisIsOriginal').text();
        var OriginalCurrency = OriginalPrice.substring(0, 1);
        OriginalPrice = OriginalPrice.substring(1);
        var total = 0;
        $('.optionPrice').each(function () {
            if ($(this).find('option:selected').attr('data-price') != 0 && $(this).find('option:selected').attr('data-price') != undefined) {
                console.log($('option:selected', this).attr("data-price"));
                total += parseFloat($('option:selected', this).attr('data-price'));
            }
        });
        var newTotal = parseFloat(OriginalPrice) + parseFloat(total);
        $('#total').text('$' + newTotal.toFixed(2));
    });
});
</script>
 

如何解决这个问题。我要选择后价格显示47,000。

【问题讨论】:

  • Fine 但选择选项后总显示 47.00 我需要显示值 47,000.00

标签: javascript php jquery opencart option


【解决方案1】:

你的代码的问题是,

  • 您的价格中包含,。因此,在 parseFloat() 之后,逗号后的值被截断。您需要在使用 parseFloat 之前删除逗号。

需要对以下几行进行更改,

 total += parseFloat($('option:selected', this).attr('data-price').replace(/,/g, ""));

var newTotal = parseFloat(OriginalPrice.replace(/,/g, "")) + parseFloat(total);

Updated Fiddle

  $('.optionPrice').change(function() {
    var OriginalPrice = $('#thisIsOriginal').text();
    var OriginalCurrency = OriginalPrice.substring(0, 1);
    OriginalPrice = OriginalPrice.substring(1);

    var total = 0;
    $('.optionPrice').each(function() {
      if ($(this).find('option:selected').attr('data-price') != 0 && $(this).find('option:selected').attr('data-price') != undefined) {
        console.log($('option:selected', this).attr("data-price"));
        total += parseFloat($('option:selected', this).attr('data-price').replace(/,/g, ""));

      }
    });
    var newTotal = parseFloat(OriginalPrice.replace(/,/g, "")) + parseFloat(total);
    $('#total').text('$' + newTotal.toFixed(2));
  });

编辑以获取逗号分隔值,

$('.optionPrice').change(function() {
  var OriginalPrice = $('#thisIsOriginal').text();
  var OriginalCurrency = OriginalPrice.substring(0, 1);
  OriginalPrice = OriginalPrice.substring(1);
  var total = 0;
  $('.optionPrice').each(function() {
    if ($(this).find('option:selected').attr('data-price') != 0 && $(this).find('option:selected').attr('data-price') != undefined) {
      console.log($('option:selected', this).attr("data-price"));
      total += parseFloat($('option:selected', this).attr('data-price').replace(/,/g, ""));
    }
  });
  var newTotal = parseFloat(OriginalPrice.replace(/,/g, "")) + parseFloat(total);
  newTotal = numberWithCommas(newTotal);
  $('#total').text('$' + newTotal + ".00");
});

function numberWithCommas(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

【讨论】:

  • 立即查看答案。
  • 已执行,但价格显示 47000 我需要逗号,我想显示 47,000.00
猜你喜欢
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 2014-04-15
  • 2015-03-24
  • 2013-11-03
  • 2017-01-14
相关资源
最近更新 更多