使用Django在提交时关闭Bootstrap模式 [英] Close Bootstrap Modal On Submit with Django

查看:81
本文介绍了使用Django在提交时关闭Bootstrap模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了该论坛上的几乎所有帖子,并且在提交后无法成功关闭我的模式. data-dismiss不起作用,因为它在开始之前停止了提交.在基本html页面中使用JavaScript代码也不起作用.我尝试过的帖子示例为此处此处.

I have been through pretty much every post on this forum and have not been successful at getting my modals to close after submission. data-dismiss does not work since it stops the submission before it starts. Using JavaScript code within the base html page also has not worked. Examples of posts that I have tried are here and here.

我的代码如下.任何建议表示赞赏.提交工作正常.莫代尔只是不会关闭.例如,请参见下面的模式"getAPI".

My code is below. Any recommendations are appreciated. Submission is working. Modal just will not close. See modal "getAPI" below for example.

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

推荐答案

您应在按钮中的form元素上声明id形式,并且该按钮应该起作用.

You should declare form id on form element not in button and it should work.

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

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

And declare button type to submit to declare it as submit button to catch submit event.

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

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

In JavaScript, you should prevent default form submission.

<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,否则<form>元素中的表单method类型和action url是多余的.

If you are handling form submit using jQuery event, form method type and action url in <form> element is redundant unless you are using DOM somewhere.

这篇关于使用Django在提交时关闭Bootstrap模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆