【问题标题】:modal dialog not dismissing bootstrap模态对话框不关闭引导程序
【发布时间】:2014-02-07 13:11:21
【问题描述】:

我在 django 模板中有以下模态对话框

search_results.html

<div class="modal fade " id="search-results" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3>Search Results:</h3>
                <h5>Click on a customer to go to customer's file</h5>
            </div>
            <div class="modal-body">
                <table class="table table-responsive table-bordered table-hover table-condensed">
                    <thead>
                        <tr>
                            <th colspan="6">Patients found {{customers.count}}</th>
                        </tr>
                        <tr>
                            <th>#</th>
                            <th>First Name</th>
                            <th>Middle Name</th>
                            <th>Last Name</th>
                            <th>Phone No</th>
                            <th>Date of Birth</th>
                        </tr>
                    </thead>
                    <tbody>
                        {%for customer in customers %}
                            <tr onClick="parent.location='{% url 'customer-edit' id=customer.id%}'">    
                                <td>{{forloop.counter}}</td>
                                <td>{{customer.first_name}}</td>
                                <td>{%if customer.middle_name%}{{customer.middle_name}}{%endif%}</td>
                                <td>{{customer.last_name}}</td>
                                <td>{{customer.telephone}}</td>
                                <td>{%if customer.birth_date%}{{customer.birth_date}}{%endif%}</td>
                            </tr>
                        {%empty%}
                            <tr>
                                <td colspan="6"><p class="text-center">No Patients Found</p></td>
                            </tr>
                        {%endfor%}
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button id="cancel" data-dismiss = "modal" class="btn btn-danger">Close</button>
            </div>
        </div><!--modal-content-->
    </div><!--modal-dialog-->
</div>

以及呈现此模板的视图

在我的网页中,我使用 ajax 发布搜索表单的数据,视图返回 html。然后我将模型对话框附加到页面上,并显示结果。

ajax 调用

var form = $(".search-patient");
$(".search-patient").on('submit', function(event){
    event.preventDefault();
    $.ajax({
        type:'post',
        url:'/customer/',
        dataType: 'html',
        data:form.serialize(),
        success: function (data, status, jqXHR){
            if ($("#search-results").length > 0){
                $("#search-results").remove();
            }//remove it if it was already there from previous search(Not the best way i know)

            $('body').append(data);
            $("#search-results").modal('show');
        }

    });
});

问题是,虽然它显示了模态框,但它不会让我通过使用 x 或关闭按钮来解雇。有什么问题?

【问题讨论】:

    标签: jquery ajax django modal-dialog twitter-bootstrap-3


    【解决方案1】:

    试试这个$("#search-results").modal('hide');应该可以。

    $('.close').click(function(){
         $("#search-results").modal('hide');
    });
    

    更新:

    var loading_dialog = $('#search-results');
    loading_dialog.modal('show');
    
     loading_dialog.modal('hide');
    

    更新: 给class="modal hide fade"

    <div class="modal fade " id="search-results" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
    

    替换为

    <div class="modal hide fade " id="search-results" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
    

    【讨论】:

    • 我认为它必须是 $(".close").on(click, function) 因为当代码正确执行时页面上不存在模式?但仍然没有工作。我从浏览器的控制台尝试了同样的事情,还是一样
    • 尝试在控制台中查看错误或在单击时关闭警报框,我认为它没有触发。
    • 仍然无法正常工作...没有错误...:(。即使您的更新也不行。显示方法有效,隐藏无效...
    • @Apostolos 尝试不显示类似$("#search-results").modal();
    • 我尝试了以下方法:将 ('body').append(data) 更改为 $('body').append("Some text") 并附加两次,就像发布了表单一样两次。发了两次,不知为什么
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多