我在index.html中有一组按钮,在views.py中有一个字典,其中包含一些按钮的值,我通过ajax导入。我想要做的是遍历字典并将每个值加载到它的特定按钮中。
这基本上就是html。
$(document).ready(function(){
$(':button').on('click', function(event){
$('#ID').val(this.id);
var tmp = this.id;
$.ajax({
type: $('#klik').attr("method"),
url : $('#klik').attr("action"),
data : $('#klik').serialize(),
success: function(d){
{% for gumb in buttons %}
alert('alo')
$('#' + Gumb.id).val(d);
{% endfor %}
//$('#' + tmp).val(d); // this works only for one button, I tried using for loop for multple
},
error: function(){
alert('Greska')
}
});
});
});至于views.py,这就是我要返回的内容。
def klik(request):
print('Test')
if request.is_ajax() and request.POST:
print(request.POST)
ID = request.POST['ID']
vr = r[ID]
gumbi = []
g = Gumb(ID, vr)
gumbi.append(g)
...something....
d = dict()
d['buttons']= gumbi
#return render(request,'index.html', d)
return HttpResponse(d)gumbi中可能有更多的Gumb,Gumb是一个类,其中Gumb.id是与html中的按钮相同的ID,而Gumb是我希望在按钮中使用的值。
它只与一个标记的按钮一起工作:
//$('#' + tmp).val(d);如果我在视图中返回以下内容:
return HttpResponse(vr)但不会有多个。
发布于 2020-04-27 06:53:45
我将假设您在这里返回一个JSON对象列表。
[{d:data},{d:data}]
//ajax code
success: function(data) {
//use $.each here not the framework each
$.each(data, function(index, value) {
console.log(value) //iterates your data see dev tools console for prints
})
}https://stackoverflow.com/questions/61448936
复制相似问题