2013-10-15 83 views
0

我正在使用Cart66,在用戶查看購物車後才能更改購物車的價格。我需要一種方法來更改當前項目的價格,以便用戶知道價格已更改。我想這可以使用jQuery/js完成,但無法弄清楚如何。使用選擇選項文本更改價格

根據「cart66-select」選項文本的「()」中的值增加/降低「cart66-price-value」的價格。

<script class="cart66-select"> 
<option value="A">Coral</option> 
<option value="B">Passion Fruit (+5.00)</option> 
</script> 
<strong class="cart66-price-value">$24.00</strong> 
+0

你試過了什麼?我認爲將價值放在價值領域會容易得多。 – Cfreak

+0

該值是Cart66用於追蹤項目的唯一值。 –

回答

0
<select class="cart66-select"> 
    <option value="A" price="10">Coral</option> 
    <option value="B" price="5">Passion Fruit (+5.00)</option> 
</select> 
<strong class="cart66-price-value">$<span id="price">24.00</span></strong> 


<script type="text/javascript"> 
    jQuery(document).ready(function(){ 
     jQuery(".cart66-select").change(function(){ 
      var price = parseInt(jQuery(this).find(':selected').attr('price')).toFixed(2); 
      jQuery("#price").html(price); 
     }); 
    }); 
</script> 
+0

使用'price.toFixed(2)'獲得兩位小數的價格 – Raidri

+0

這很好用,與我的相似。但是,Cart66不允許添加自定義屬性。 –

0

可以使用jQuery是@Aaron來完成。看看example

<select class="cart66-select"> 
    <option value="$24.00">Coral</option> 
    <option value="$29.00">Passion Fruit (+5.00)</option> 
</select> 
<strong class="cart66-price-value">$24.00</strong> 

var changePrice = function() 
{ 
    var select = $(".cart66-select"), 
     displayPrice = $(".cart66-price-value"); 

    select.change(function(){ 
     var selected = $(this).children("option:selected").val(); 
     displayPrice.text(selected); 
    }); 
} 

changePrice(); 
+0

這完美的作品!但是,因爲我正在使用Cart66,所以value屬性已被用於使用唯一標識符跟蹤項目。我需要的是一種使用「(+5.00)」來提高價格的方法。 –

+0

現在我明白了真正的問題。在這種情況下,正則表達式解決!試試這個http://jsfiddle.net/Ra4dY/2/ – marceloogeda

+0

你是一個天才!正是我在找什麼。乾杯! –

相關問題