【问题标题】:Passing Dropdown Choice to Modal Using Django For Loop使用 Django For 循环将下拉选择传递给模态
【发布时间】: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


【解决方案1】:

您需要执行另一个循环并为每个choices 创建一个.modal

    <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>

  {% for choice in choices %}

    <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>You selected Choice {{ choice }}</p>
            </div>
        </div>
    </div>   

  {% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 2020-04-02
    • 1970-01-01
    • 2018-09-10
    • 2014-08-11
    • 2019-09-16
    • 2012-07-20
    • 2021-09-05
    相关资源
    最近更新 更多