django - 如何在 Popup 中显示 Ajax 响应数据?
问题描述
我有一些数据,response
但我想在弹出窗口中显示该数据,请告诉我如何在弹出窗口中显示 Ajax 数据。
这是我的views.py
文件...
def myview(request):
datas=TestForm.objects.all
template_name='test.html'
context={'datas':datas}
return render(request, template_name, context)
def myview(request, id):
display=TestForm.objects.get(pk=id)
template_name='test.html'
context={'display':display}
return render(request, template_name, context)
这是我的html
文件...
{% for a in datas %}
<a href="javascript:void()" class="btn btn-primary" onclick"exampleModal({{a.id)}})" data-url="{% url 'myap:list_single' a.id %}">
{{a.product_id}}
</button>
{% endfor %}
这是我的popup
代码...我想在哪里显示 AJAX 数据...
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-
labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<tr>
<td>{{datas.name}}</td>
<td>{{datas.price}}</td>
<td>{{datas.category}}</td>
</tr>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
这是我的AJAX
代码...
function exampleModal(id){
$.ajax({
url: $(this).attr("data-url")
type: 'get',
dataType: "HTML"
success: function(res) {
$('.exampleModal').html(res);
$("#exampleModal").modal("show");
}
});
}
解决方案
function exampleModal(id){
$.ajax({
url: $(this).attr("data-url")
type: 'get',
dataType: "HTML"
success: function(res) {
$('#exampleModal .modal-body').html(res); //here is where the response is added to the body element of the modal
$("#exampleModal").modal("show");
}
});
}
推荐阅读
- python - 如何以 Python 中的日期时间格式将结束日期设为开始日期前 11 个月?
- java - 如何使用泛型来提供“通用 getter 方法”?
- php - 如何在邮递员响应中解决您没有访问端点所需的范围
- node.js - 想要在特定的时间间隔将邮件安排给 n 使用 node.js 的人吗?
- regex - 将 CFG 转换为正则表达式
- php - 我可以在 apatch 中的 php 文件中播放位于硬盘中的文件路径中的视频吗?
- angular - 如何修补嵌套表单数组?
- rust - 为什么 max_by_key 与 i32.log10() 导致“尝试添加溢出”
- python - 将华氏温度转换为摄氏度,反之亦然 python
- python - 如何装饰 Python 进程以捕获 HTTP 请求和响应?