我是 Django 的初學者,我創建了結帳頁面,當用戶更改輸入范圍編號時,在 此處輸入影像描述,它會發送到服務器,但它適用于第一個我認為是因為我為所有輸入設定了相同的 ID,我無法更改它,因為它用于標記
我該如何解決?
姜戈部分
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax():
count=request.GET['count']
return JsonResponse({'count':count},status=200)
阿賈克斯部分
$(document).ready(function(){
var crf=$('input[name=csrfmiddlewaretoken]').val();
$("#icount").change(function(){
$.ajax({
url:'/adding',
type:'get',
data:{
count:$('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
});
});
});
html部分
<tbody>
{% for order in order.orderdetail_set.all %}
<tr>
<td class="product-image">
{% thumbnail order.product.image "100x100" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
</td>
<td class="product-name"><a href="products/{{order.product.id}}">{{order.product.title}}</a></td>
<td class="product-price">{{order.product.price|intcomma:False}}?????</td>
<td class="product-quantity">
<label>?????</label>
<input type="hidden" name="order_id{{order.id}}" value="{{order.id}}">
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number">
</td>
<td class="product-total">{{order.totalprice|intcomma:False}}</td>
<td class="product-remove"><a href="delete/{{order.id}}"><i class="fa fa-trash-o"></i></a></td>
</tr>
{% endfor %}
</tbody>
uj5u.com熱心網友回復:
你可以做這樣的事情
<input name='count' id='icount' min="1" max="100" value="{{order.count}}" type="number" onchange="handleOrderCount({{order.id}})">
function handleOrderCount(OrderId){
$.ajax({
url:'/adding',
type:'PUT',
data:{
'OrderId': OrderId,
'csrfmiddlewaretoken': '{{ csrf_token }}',
'count': $('#icount').val(),
},
success:function(respond){
$('#icount').val(respond.count)
}
error:function(respond){
console.log(respond)
}
});
}
如果您想更改資料庫中的任何內容,請不要使用GET方法,它用于從資料庫或服務器中檢索資料PUT以更新任何物件
POST - create
GET - read
PUT - update
DELETE - delete
在你看來,你可以像這樣
class Ajax_Handler(View):
def get(self,request):
if request.is_ajax() and request.method == "POST":
count = request.POST['count']
# can filter object and update it like this
# order_id = request.POST['OrderId']
# Order.object.filter(id=order_id)
return JsonResponse({'count':count},status=200)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386014.html
上一篇:自動完成視窗沒有彈出
