2014-11-03 36 views
0

我想總結一些來自多個選擇框的值。問題是這些選項包含文本和數字。我只需要這些數字並將其總結起來。沒有其他辦法可以做到這一點。 所以我試圖從字符串中去掉文本,然後每次更改選擇框時更新金額。現在結果總是錯誤的。總結多個選擇框的值

我的另一個問題是,有時在號碼前有一個減號。有什麼方法可以從總體中提取出來嗎?在小提琴的第三個選擇框中有一個例子。

任何人都可以幫忙嗎?我創建了一個Fiddle here

HTML

<form method="post" id="product_configure_form" action="cart/add/14161745"> 
    <div class="product-info-options tui"> 
     <input type="hidden" value="" id="product_configure_bundle_id" name="bundle_id"> 
     <div class="product-configure"> 
      <div class="product-configure-custom"> 
       <div class="product-configure-custom-option"> 
        <label for="product_configure_custom_604623">Stoffering: <em>*</em> 

        </label> 
        <select id="product_configure_custom_604623" name="custom[604623]"> 
         <option selected="selected" disabled="disabled" value="">Maak een keuze...</option> 
         <option value="5217777">Camira 24/7 *snellever - 2 weken</option> 
         <option value="5217779">Staccato *snellever - 2 weken (+€27,00)</option> 
         <option value="5217781">Leer 1 Trento *snellever - 2 weken (+€85,00)</option> 
         <option value="5217783">Leer 2 Litano (+€172,00)</option> 
        </select> 
        <div class="product-configure-clear"></div> 
       </div> 
       <div class="product-configure-custom-option"> 
        <label for="product_configure_custom_603895">Armlegger frame kleur: <em>*</em> 

        </label> 
        <select id="product_configure_custom_603895" name="custom[603895]"> 
         <option selected="selected" disabled="disabled" value="">Maak een keuze...</option> 
         <option value="5217785">zwart gecoat</option> 
         <option value="5217787">aluminium gecoat (+€11,00)</option> 
         <option value="5217789">aluminium gepolijst (+€11,00)</option> 
         <option value="5217791">verchroomd (+€53,00)</option> 
        </select> 
        <div class="product-configure-clear"></div> 
       </div> 
etc............ 

JQUERY

 function update_amounts() { 
      var sum = 0.0; 
      $('#product_configure_form .product-configure-custom-option select').each(function() { 
      var price = $(this).find('option:selected').text(); 
      var priceClean = price.replace(/[^0-9]/g, ''); 
      priceClean = parseFloat(priceClean) || 0; 
      sum += priceClean; 
      }); 
      $('#amount').text('€' + sum.toFixed(2)); 
     } 


     $("#product_configure_form .product-configure-custom-option select").change(function() { 
      update_amounts(); 
     }); 
+1

將數字放在'value'屬性中,並使用它,而不是文本。 – Barmar 2014-11-03 23:01:06

+0

你能否詳細說明爲什麼「沒有其他方法可以做到這一點」?有什麼限制? – Fiddles 2014-11-03 23:40:47

回答

2

假設你不能在所有更改HTML結構,並且最後一個括號包含價格(如果有):

function update_amounts() { 
    var sum = 0.0; 
    $('#product_configure_form .product-configure-custom-option select').each(function() { 
     var optionText = $(this).find('option:selected').text(); 
     var matches = optionText.match(/\(([^)]+)\)/g) || []; 
     if (matches.length > 0) { 
      var priceText = matches[matches.length - 1]; 
      if (priceText.indexOf("€") > -1) { 
       var priceClean = priceText.replace(/[^0-9\+\-\.\,]/g, ''); 
       var priceValue = parseFloat(priceClean) || 0; 
       sum += priceValue; 
      } 
     } 
    }); 
    $('#amount').text('€' + sum.toFixed(2)); 
} 

$("#product_configure_form").on("change", ".product-configure-custom-option select", update_amounts); 

您可能需要廣告只是「priceClean」正則表達式取決於您的小數點字符。

+0

這就是我的意思。我無法更改此解決方案的html ... thx! – Meules 2014-11-04 09:47:36

3

如果你不想把這些數字的值,那麼你可以使用HTML data attributes
所以,你可以把你的選擇框是這樣的:

<select id="mySelectBox"> 
    <!--selected as an example--> 
    <option value="123" data-price="27000" selected>Some Stuff ($27,000)</option> 
    <option value="124" data-price="12000">Magic Sword ($12,000)</option> 
</select> 

,然後使用jQuery,您可以提取使用data attributejQuery.data()

//27,000 
var price = $('select#mySelectBox').find(':selected').data('price'); 
+0

Thx這是我的正常途徑,但我無法更改代碼中的任何內容。 – Meules 2014-11-04 10:00:19