Javascript 无法了解如何在django电子商务网站中处理产品大小,因为不同产品的选择框具有相同的id

Javascript 无法了解如何在django电子商务网站中处理产品大小,因为不同产品的选择框具有相同的id,javascript,django,Javascript,Django,这是我的html: {% if product.id in list_cart %} <div class="btn-group" style="display: none;"> <select class="selection-2 border" data-product={{product.id}} style="border: cadetblue;" name="size"

这是我的html:

{% if product.id in list_cart %}
<div class="btn-group" style="display: none;">
<select class="selection-2 border" data-product={{product.id}} style="border: cadetblue;"
 name="size" required id="sizebox">
 {% for t in product.size.all %}
<option value="{{t}}" id="{{t}}">{{t}}</option>
 {% endfor %}
  </select>
  </div>
    {% else %}
 <div class="btn-group">
 <select class="selection-2 border" data-product={{product.id}} style="border: cadetblue;"
    name="size" required id="sizebox">
 {% for t in product.size.all %}
 <option value="{{t}}" id="{{t}}">{{t}}</option>
  {% endfor %}
</select>
  </div>
{% endif %}
{%if列表中的product.id\u cart%}
{product.size.all%中t的%
{{t}
{%endfor%}
{%else%}
{product.size.all%中t的%
{{t}
{%endfor%}
{%endif%}
这是我的javascript:

var updateBtns = document.getElementsByClassName('update-cart')

for (i=0;i<updateBtns.length;i++){
updateBtns[i].addEventListener('click',function(){
    var productId=this.dataset.product
    var action=this.dataset.action


    var sizebox = document.getElementById("sizebox");
    var size = sizebox.options[sizebox.selectedIndex].value;
    console.log(size)


    updateUserOrder(productId, action, size)
   
})
}
var updateBtns=document.getElementsByClassName('update-cart'))

对于(i=0;i尝试使用循环计数器并为选择字段生成不同的id, 使用loop.index生成值或使用自定义计数器,如

counter = 0
{%for ...%}
<option value="{{t}}" id="{{t+counter}}">{{t}}</option>
counter++
{%endfor%}
计数器=0
{对于…%}
{{t}
柜台++
{%endfor%}
或者使用loop.index,这是您的代码

{% if product.id in list_cart %}
<div class="btn-group" style="display: none;">
<select class="selection-2 border" data-product={{product.id}} style="border: cadetblue;"
 name="size" required id="sizebox">
 {% for t in product.size.all %}
<option value="{{t}}" id="{{t+loop.index}}">{{t}}</option>
 {% endfor %}
  </select>
  </div>
    {% else %}
 <div class="btn-group">
 <select class="selection-2 border" data-product={{product.id}} style="border: cadetblue;"
    name="size" required id="sizebox">
 {% for t in product.size.all %}
 <option value="{{t}}" id="{{t+loop.index}}">{{t}}</option>
  {% endfor %}
</select>
  </div>
{% endif %}
{%if列表中的product.id\u cart%}
{product.size.all%中t的%
{{t}
{%endfor%}
{%else%}
{product.size.all%中t的%
{{t}
{%endfor%}
{%endif%}

那么我如何修改我的javascript以获取collect值呢?我还认为问题在于select标记的id,而不是option标记的id,因为在我的javascript中,我使用的是sizebox id。您可以将产品id和sizebox连接起来,生成新的id,或者使自定义计数器和用户选择框