如何通过 Shopify 响应式获取 <input> 的值?

How can I obtain the value of <input> responsively with the Shopify?

我想给产品添加google标签管理,方便管理。所以我想得到客户购买的产品。但是当我想获取产品的数量时,我不知道如何响应地获取它。所以每次用户改号,我都能拿到。 这是 Shopify 提供的代码:

<div class="product-form__item product-form__item--quantity">
  <label for="Quantity-{{ section.id }}">{{ 'products.product.quantity' | t }}</label>
  <input type="number" id="Quantity-{{ section.id }}" name="quantity" value="1" min="1" class="product-form__input" pattern="[0-9]*" data-quantity-input>
</div>

这是我要获取的数据:

<button onclick="dataLayer.push{"event":"addTocart","currencyCode":"",'products': [{
  "id": "{{product.id}}",
  "name": "{{ product.title }}",
  "price": {{ product.price | divided_by: 100.00 | json }},
  "quantity": {{data-quantity-input}}}]}" 
  type="submit" 
  name="add"
  {% unless current_variant.available %} aria-disabled="true"{% endunless %}
  aria-label="{% unless current_variant.available %}{{ 'products.product.sold_out' | t }}{% else %}{{ 'products.product.add_to_cart' | t }}{% endunless %}"
  class="btn product-form__cart-submit{% if section.settings.enable_payment_button %} btn--secondary-accent{% endif %}"
  data-add-to-cart>

我相信您需要使用以下 onclick 属性:

<button 
  onclick='dataLayer.push({"event":"addTocart","currencyCode":"","products": [{
    "id": "{{product.id}}",
    "name": "{{ product.title }}",
    "price": {{ product.price | divided_by: 100.00 | json }},
    "quantity": document.querySelector("[data-quantity-input]").value}]})'
  ...
  other element attirubtes
  ...
>Add</button>