gpt4 book ai didi

javascript - 需要从表单中获取选定的选项值并通过 jquery/javascript 传回

转载 作者:行者123 更新时间:2023-12-03 06:27:17 26 4
gpt4 key购买 nike

我需要动态获取用户选择的任何选项的值,并在单击按钮时将该值传递回 Facebook Pixel。

请参阅脚本区域中的所选选项值

      <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>

最佳答案

您可以获得select元素的选定选项值 through the .val() function with jQuery因为您已经使用了该库。

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

或者,因为 id 似乎是生成的:$('select.product-single__variants').val()

您的最终 JavaScript 应如下所示:

 $('#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}},
});
});

关于javascript - 需要从表单中获取选定的选项值并通过 jquery/javascript 传回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38596745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com