Django cart.js循环未运行

2024-09-30 23:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

cart.js 这不起作用 控制台中未打印任何内容或某些声明有误 迅速回答

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

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

    })
}    

html代码中有错误吗

 {% for product in products %}
    {% if product.category == "Dosa" %}
    <div class="product">        
    <tr>
      <td><h5>{{ product.name }}</h5></td>
      <td><h5>{{ product.price }}</h5></td>
      <td><button data-product={{product.id}} data-action="add" class="btn btn-warning btn-add update-cart">Add to cart</button></td>
    </tr>
    </div>
    {% endif %}
    {% endfor %}

Tags: divforvarupdateactionproductthisdataset