【问题标题】:Close Bootstrap Modal On Submit with Django使用 Django 提交时关闭引导模式
【发布时间】:2019-07-20 02:30:47
【问题描述】:

我已经浏览了这个论坛上的几乎所有帖子,但在提交后未能成功关闭我的模态。 data-dismiss 不起作用,因为它在开始之前停止提交。在基本 html 页面中使用 JavaScript 代码也没有奏效。我尝试过的帖子示例有herehere

我的代码如下。任何建议表示赞赏。提交工作。模态只是不会关闭。例如,请参阅下面的模态“getAPI”。

<!DOCTYPE html>
{% load static %}
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap-theme.min.css">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
    <div id="spacer">
    </div>
    <div style="padding-left:20px;">
        <button type = "button" class="btn" data-toggle="modal" data-target="#filetypeModal">Get Device Data</button>
    </div>
    <!-- File Type Modal -->
    <div class="modal fade" id="filetypeModal" tabindex="-1" role="dialog" aria-labelledby="filetypeModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <form action="" method="post">
                    {% csrf_token %}
                    <div class="modal-header" style="background-color: darkblue; color: white">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="color: white">&times;</button>
                        <h4 class="modal-title" id="filetypeModalLabel">File Types</h4>
                    </div>
                    <div class="modal-body" style="text-align: center;">
                        Choose file type to download.<br>
                    <br>
                        <label class="radio-inline"><input type="radio" name="choices" value="Excel" checked>Excel</label>
                        <label class="radio-inline"><input type="radio" name="choices" value="CSV">CSV</label>
                    </div>
                    <div class="modal-footer" style="background-color: darkblue;">
                        <input type="submit" class="btn btn-primary" id="getAPI" value="OK" name="getAPI"></input>
                    </div>
                </form>
            </div>
        </div>
    </div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
    $("#getAPI").submit(function() {
        $("#filetypeModal").modal("hide");
    });
</script>

【问题讨论】:

    标签: javascript jquery django bootstrap-modal


    【解决方案1】:

    可能有用的东西: 按照建议创建按钮:

    <button type="button" id="closeModal" class="btn btn-danger" data-dismiss="modal">Close</button>
    

    并使用

    $("#getAPI").submit(function() {
       $("#closeModal").click(event =>{
        alert('Success');
       });
    });
    

    【讨论】:

    • 我希望它在点击提交按钮后发生,以尽量减少用户点击...
    • 我会编辑答案,但基本上是一样的:)
    【解决方案2】:

    您应该在 form 元素上声明表单 id 不在按钮中,它应该可以工作。

    <form id="getAPI">
    [...]
    </form>
    

    并声明要提交的按钮类型以将其声明为提交按钮以捕获提交事件。

    <button type="submit" .....>Save</button>
    

    在 JavaScript 中,您应该阻止默认表单提交。

    <script>
        $("#getAPI").submit(function(event) {
        $.ajax({
            url: '/',  // your url endpoint
            type: 'POST',
            data: $('#getAPI').serialize(),  // gather form data
            success: function(data) {
                    console.log(data);
                    $("#filetypeModal").modal("toggle");  // Use toggle to close modal
            }  // end success callback
         });  // end ajax call
        event.preventDefault();  // prevent default submission
        });
    
    </script>
    

    如果你正在使用 jQuery 事件处理表单提交,除非你在某处使用 DOM,否则 &lt;form&gt; 元素中的表单 method 类型和 action url 是多余的。

    【讨论】:

    • 这会关闭模式,但不会完成提交。
    • 你使用ajax提交吗?
    • 您应该在表单提交完成后关闭模式。有关要点,请参阅我的更新答案。
    • 现在正在提交但没有关闭模式。
    • 提交日志数据到控制台吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2015-06-27
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2014-06-30
    相关资源
    最近更新 更多