Jquery - 提高价格

时间:2017-12-17 15:40:21

标签: jquery html

我正试图在文中将产品价格提高+ 200。重要的是价格号旁边的货币需要留在那里。

<div class="col-md-4 product text-center" style="display: 
  <a href="/product/vintage-stribrne-pirko">
    <div class="product-img" style="background: ...') center center no-repeat; background-size: contain;">
    </div>
  </a>
  <div class="product-title">
    <a href="/product/vintage-stribrne-pirko">Vintage stříbrné pírko</a>
  </div>
  <div class="product-price"> 490  Kč</div>
</div>

我的Jquery代码仅在我将productPrice放入console.log:

时才有效
jQuery(document).ready(function($) {   
  $('.product').each(function() {
  var productPrice = parseInt($(this).find('.product-price').text()) + 2000;
  $(productPrice).text();
  });
});

非常感谢你。非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

$(productPrice).text();

你在这里混淆了一些事情。您应该将新值传递给text(),而不是选择器。

你的意思是:

$('.product-price').text(productPrice);

不带参数调用text()会返回当前值;用一个调用它设置一个新值。

答案 1 :(得分:1)

这对你有帮助吗?

因此我们使用JQuery从div中获取价格并剥离货币并最终添加值以在类output的另一个div中显示结果,如果这可以解决您的问题,请告诉我!

$(document).ready(function($) { 
  var price = 0;
  $('.product').each(function() {
  price += parseInt($(this).find('.product-price').text().replace("Kč", "").replace(" ", ""))  + 2000;
  });
  $(".output").text(price + " Kč");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4 product text-center">
  <a href="/product/vintage-stribrne-pirko">
    <div class="product-img">
    </div>
  </a>
  <div class="product-title">
    <a href="/product/vintage-stribrne-pirko">Vintage stříbrné pírko</a>
  </div>
  <div class="product-price"> 490  Kč</div>
</div>
<div class="output"></div>

答案 2 :(得分:0)

好的,让它变得简单

jQuery(document).ready(function($) {   
  $('.product').each(function() {
  var ProductPriceElement = $(this).find('.product-price'), // the product-price element
      producttext = ProductPriceElement.text(), // element text
      productPrice = parseInt(producttext),    // element price number
      productPricePlus200 = productPrice + 200;  // add 200 to the price number
  var NewTextReplace = producttext.replace(productPrice , productPricePlus200); // replace the old price with the new one
  ProductPriceElement.text(NewTextReplace); // change the element text with the new one
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4 product text-center" style="display: block">
  <a href="/product/vintage-stribrne-pirko">
    <div class="product-img" style="background: ...') center center no-repeat; background-size: contain;">
    </div>
  </a>
  <div class="product-title">
    <a href="/product/vintage-stribrne-pirko">Vintage stříbrné pírko</a>
  </div>
  <div class="product-price"> 490  Kč</div>
</div>

相关问题