2016-07-26 3 views
0

Ich muss den Wert der Option, die von einem Benutzer ausgewählt wurde, dynamisch abrufen und diesen Wert zurück an Facebook Pixel auf Schaltfläche klicken.Sie müssen Selected Option Wert aus einem Formular greifen und über jquery/javascript zurückgeben

Siehe AUSGEWÄHLTER OPTIONSWERT im Skriptbereich.

 <form action="/cart/add" method="post" enctype="multipart/form-data" class="AddToBagForm text-center medium-up--text-left" novalidate>  
     <select name="id" id="product-select-2585865157" class="product-single__variants"> 
       <option selected="selected" value="7534116741">White Marble/S - $39.95 USD</option> 
       <option value="7534116869">White Marble/L - $39.95 USD</option> 
       <option value="7534117061">Midnight/S - $39.95 USD</option> 
       <option value="7534117253">Midnight/L - $39.95 USD</option> 
       <option value="9032311493">Maroon/M - $39.95 USD</option> 
       <option value="9032318853">Maroon/L - $39.95 USD</option> 
     </select> 
     <div class="add-container clearfix"> 
      <a href="#" class="btn--primary BIS_trigger" data-product-handle="mighty-ganesh-flowy-top" data-variant-id="7534116741">Get Notified</a> 
      <div class="quantity-field"> 
      <label for="Quantity" class="quantity-selector">Qty:</label> 
      <input type="number" id="Quantity" name="quantity" value="1" min="1" max="3" class="quantity-selector"> 
      </div> 
      <button type="submit" name="add" class="AddToBag btn--primary" id="add-to-cart" > 
      <span class="AddToBagText">Add to Bag</span> 
      </button> 
     </div> 

     <script type="text/javascript"> 
      $('#add-to-cart').click(function() { 
      fbq('track', 'AddToCart', { 
       content_ids: '{{ product.id }}.' + **SELECTED OPTION VALUE**, 
       content_type: 'product', 
       value: {{ product.price | money_without_currency}}, 
      }); 
      }); 
     </script> 

     </form> 

Antwort

1

können Sie erhalten einen through the .val() function with jQuery Optionswert ausgewählt ist select Element, da Sie bereits die Bibliothek benutzen.

$('#product-select-2585865157').val()

Oder, denn die id erzeugt zu sein scheint: $('select.product-single__variants').val()

Ihre letzte JavaScript sollte wie folgt aussehen:

$('#add-to-cart').click(function() { 
     var selection = $('#product-select-2585865157').val(); 
     /* var selection = $('select.product-single__variants').val(); */ 

     fbq('track', 'AddToCart', { 
      content_ids: '{{ product.id }}.' + selection, 
      content_type: 'product', 
      value: {{ product.price | money_without_currency}}, 
     }); 
}); 
+0

Dank. Ich werde es in Kürze überprüfen. Eine kurze Folgefrage: Die Produkt-Auswahl # ist dynamisch und verwendet eine der internen Variablen von Shopify: