Javascript Shopify适合库存的数量下拉列表

Javascript Shopify适合库存的数量下拉列表,javascript,html,css,shopify,liquid,Javascript,Html,Css,Shopify,Liquid,我有一家shopify商店。如何匹配数量下拉列表以匹配实际库存数量。例如,如果有3个项目,“数量”下拉列表的最大值应为3。如果有5项,则最大值应为5 谢谢, Alex如果您想在产品页面中添加数量下拉列表,下面有一个示例演示如何执行此操作: <label for="quantity">Qty: </label> <select id="quantity" name="quantity"> {% for i in (1..10) %} <option val

我有一家shopify商店。如何匹配数量下拉列表以匹配实际库存数量。例如,如果有3个项目,“数量”下拉列表的最大值应为3。如果有5项,则最大值应为5

谢谢,
Alex

如果您想在产品页面中添加数量下拉列表,下面有一个示例演示如何执行此操作:

<label for="quantity">Qty: </label>
<select id="quantity" name="quantity">
{% for i in (1..10) %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
否则,最好限制购物车中的数量。在cart.liquid中尝试此操作:

<select id="updates_{{ item.id }}" name="updates[]"> 
{% for i in (1..item.variant.inventory_quantity) %}
<option value="{{ i }}"{% if item.quantity == i %} selected{% endif %}>{{ i }}</option>
{% endfor %}
</select>

{(1..item.variant.inventory_quantity)%%中i的%
{{i}
{%endfor%}

我将此功能移动到jQuery,并从产品页面中删除了液体。换句话说,我使用了一个类似Steph Sharp提到的实现,但是遇到了她提到的限制。我的数量
select
元素现在为空:

<select id="quantity" name="quantity"></select>

使用jQuery,我实现了跟踪库存的数量下拉列表:

for(var counter = 1; counter <= variant.inventory_quantity; counter++){
  if(counter === 1){
    $('#quantity').empty();
  }
  var newQtyOption = $("<option></option>").val(counter).html(counter);
  $('#quantity').append("<option value='" + counter + "'>" + counter + "</option>")
}
for(变量计数器=1;计数器
for(var counter = 1; counter <= variant.inventory_quantity; counter++){
  if(counter === 1){
    $('#quantity').empty();
  }
  var newQtyOption = $("<option></option>").val(counter).html(counter);
  $('#quantity').append("<option value='" + counter + "'>" + counter + "</option>")
}