【发布时间】:2021-08-28 01:47:26
【问题描述】:
我有一个带有选项的下拉菜单。下拉菜单应链接到我想说“您选择了选项 A”(或 B 或 C)的模式,具体取决于单击的内容。
我尝试了data-target="#myModal{{choice}}" 和id="myModal{{choice}}",希望它们能被链接,但这样一来,模态框根本不会出现。
当我删除 {{choice}} 时会出现模式,但这不是我想要的。
感谢阅读。非常感谢您的帮助。
views.py:
def modal_home(request):
choices = [
"A",
"B",
"C"
]
return render(request, 'modal/test-modal.html', {
'choices': choices,
})
test-modal.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown">
Create
</button>
<div class="dropdown-menu">
{% for choice in choices %}
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#myModal{{choice}}">{{ choice }}</a>
{% endfor %}
</div>
</div>
<div class="modal fade" id="myModal{{choice}}" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">MODAL</h5>
<button type="button" class="close" data-dismiss="modal">
</button>
</div>
<p>Here I want to say "You selected Choice A"</p>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
你需要做另一个循环并为每个
choices创建一个.modal -
@ArleighHix 是的,用另一个循环包裹模态 div 是可行的。非常感谢
标签: html django bootstrap-5