18910140161

使用Django For循环将下拉选项传递给模态

顺晟科技

2021-08-27 12:02:25

210

我有一个带有选项的下拉列表。下拉菜单应该链接到一个模式,在该模式中,我想说“您选择了选择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>

顺晟科技:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航