这是我的cart.js:
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
console.log('productId:',productId,'Action:',action)
console.log('user:',user)
updateUserOrder(productId, action)
})
}
function updateUserOrder(productId,action){
console.log('User is logged In , sending data....')
var url = "/update_item/"
fetch(url, {
method:'POST',
headers:{
'Content-Type':'application/json',
'X-CSRFToken':csrftoken,
},
body:JSON.stringify({'productId':productId,'action':action})
})
.then((response) =>{
return response.json()
})
.then((data) =>{
console.log('data:',data)
location.reload()
})
}
这是我的按钮:
<button data-product={{product.id}} data-action="remove" class="flex-c-m sizefull bg1 bo-rad-23 hov1 s-text1 trans-0-4 update-cart">
Added<sub>Remove</sub>
</button>
我想将类似的属性应用于此选择框:
<select class="selection-2" name="size" required id="sizebox">
{% for t in product.size.all %}
<option value="{{t}}" id="{{t}}">{{t}}</option>
{% endfor %}
</select>
我想给它类似的属性,这样我就可以使用javascript将其数据发送到我的views.py:
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
我尝试获取所选的选项值,并在我的views.py中执行了size = data ['size'],但是遇到了关键错误。最好的发送方式是什么?不胜感激。谢谢