django - 确认删除的引导模式
问题描述
当我遍历我的图像时,我有一个图标,您可以单击该图标来删除图像。相反,我希望弹出一个模式来确认删除。唯一的问题是我不想为每个图像重复模态代码。我需要一种将图像 ID 传递给模态的方法。
我在想我可以通过 id 传递,onClick
但我不确定它是否适用于 Bootstrap 模态。
这是我目前删除的方式:
{% for image in images %}
<img src="{{image.image.url}}">
<form method="POST" action="{% url 'products:delete_image>
{% csrf_token %}
<button type="submit" rel="tooltip" title="Remove">
<i class="material-icons">close</i>
</button>
</form>
{% endfor %}
这是我要集成的模态代码
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">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">
...
</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>
</div>
</div>
解决方案
为每个表单内的按钮分配一个点击处理程序,并为每个表单分配一个唯一的 id。例如,给所有表单按钮一个类,如confirm-delete
. 分配一个点击处理程序(这是 jQuery,但你也可以使用 vanilla JS)...
$('.confirm-delete').on('click', function(e) {
// prevent form submit
e.preventDefault();
// get the current image/form id
var id = $(this).attr("id")
// assign the current id to the modal and show the modal
$('#myModal').data('id', id).modal('show');
})
然后在模态框内,有一个按钮,它使用传递的 id 实际执行删除data
...
$('#btnDelete').click(function() {
// handle deletion here
var id = $('#myModal').data('id');
// submit the form
$('form'+id).submit();
// if needed, remove deleted image
// and form from DOM
$('#img'+id).remove();
$('#form'+id).remove();
// hide modal
$('#myModal').modal('hide');
})
推荐阅读
- reactjs - 如何在 reactcommerce 的个人资料页面中隐藏 TagNav
- react-native - 没有一个反应原生教程来集成谷歌地图作品
- php - 在wordpress中取消进程wp远程获取请求
- android - 支持 BlackBerry 10 的 Android NFC 应用程序
- ios - 在容器视图中调整子视图控制器的大小(自动布局)
- sap-gui - SAP HANA Studio 更改 SAP GUI 分辨率
- google-colaboratory - 将秘密变量传递给 Google Colaboratory notebook
- javascript - req.body 在快递的 307 重定向上获得两次
- google-apps-script - 谷歌表格和谷歌分析认证
- java - 有什么方法可以将字符串 [这是某个类的对象] 转换为某个类的对象吗?