计算产品列表页面上多个产品的百分比折扣

问题描述

您好,我正在尝试计算产品列表页面上销售的产品的折扣百分比,这些产品具有过去和现在的价格。

我已经使用代码来获得百分比折扣,但我似乎无法让它为每个产品动态工作。目前,它会运行计算,然后为每个产品打印相同百分比的折扣,甚至是全价产品。

对此的任何帮助将不胜感激。谢谢!

setTimeout(function discountPrice() {
  $('.product-slab__price').each(function() {
    var pattern = /[^0-9\.]/g;
    var wasprice = parseFloat($(".product-price__primary .product-price__was").text().replace(pattern,"")) / 100;
    var newPrice = parseFloat($(".product-price__primary .product-price__value--discounted").text().replace(pattern,"") / 100);
    var subtractPrice = (Math.abs(wasprice - newPrice));
    var dividePrice = (Math.abs(subtractPrice / wasprice));
    var multiplyPrice = (Math.abs(dividePrice * 100));
    $('.discountPercentage').html(multiplyPrice.toFixed(0) + "&#37");
  });
},500);
.product-price__was {
  text-decoration: line-through;
}

.product-price__value.product-price__value--discounted {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__was">Was &euro;10,00</span>
    <span class="product-price__value product-price__value--discounted">Now &euro;8,00</span>
    <span class="discountPercentage" > </span>
  </div>
</div>

<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__was">&euro;15,00</span>
    <span class="product-price__value product-price__value--discounted">&euro;10,00</span>
    <span class="discountPercentage"></span>
  </div>
</div>


<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__value">&euro;15,90</span>
    <span class="discountPercentage"></span>
  </div>
</div>

解决方法

问题是因为当您在循环中进行迭代时,您正在使用这些类选择 DOM 中的所有元素。

相反,您需要使用与迭代中的当前 .product-slab__price 相关的那些类来定位元素。为此,请使用 this 关键字和 find() 方法:

setTimeout(function discountPrice() {
  var pattern = /[^0-9\.]/g;

  $('.product-slab__price:has(.product-price__value--discounted)').each(function() {
    let $slab = $(this);
    var wasPrice = parseFloat($slab.find(".product-price__primary .product-price__was").text().replace(pattern,"")) / 100;
    var newPrice = parseFloat($slab.find(".product-price__primary .product-price__value--discounted").text().replace(pattern,"") / 100);

    var subtractPrice = Math.abs(wasPrice - newPrice);
    var dividePrice = Math.abs(subtractPrice / wasPrice);
    var multiplyPrice = Math.abs(dividePrice * 100);
    
    $slab.find('.discountPercentage').html(multiplyPrice.toFixed(0) + "&#37");
  });
},500);
.product-price__was {
  text-decoration: line-through;
}

.product-price__value.product-price__value--discounted {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__was">Was &euro;10,00</span>
    <span class="product-price__value product-price__value--discounted">Now &euro;8,00</span>
    <span class="discountPercentage"> </span>
  </div>
</div>

<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__was">&euro;15,00</span>
    <span class="product-price__value product-price__value--discounted">&euro;10,00</span>
    <span class="discountPercentage"></span>
  </div>
</div>


<div class="product-slab__price">
  <div class="product-price__primary">
    <span class="product-price__value">&euro;15,90</span>
    <span class="discountPercentage"></span>
  </div>
</div>

请注意,由于正则表达式内容是静态的,因此可以在循环外定义。

,

你需要

  • 使用,this保持在当前slab的范围内
  • 测试乘法价格是一个数字
  • 移动正则表达式,因为您不需要每次都创建它

不需要

  • 通过您显示的代码中的 .product-price__primary
  • parseFloat 因为你没有在任何地方添加。如果您确实需要添加
  • ,可以使用一元加号 +wasPrice

const pattern = /[^0-9\.]/g,discountPrice = function() {
    $('.product-slab__price').each(function() {
      const wasPrice = $(".product-price__was",this).text().replace(pattern,"") / 100,newPrice = $(".product-price__value--discounted",subtractPrice = Math.abs(wasPrice - newPrice),dividePrice = Math.abs(subtractPrice / wasPrice),multiplyPrice = Math.abs(dividePrice * 100);

      if (!isNaN(multiplyPrice)) $('.discountPercentage',this).html(multiplyPrice.toFixed(0) + "&#37");
    });
  };

setTimeout(discountPrice,90</span>
    <span class="discountPercentage"></span>
  </div>
</div>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...