模态不会显示但单击按钮时会出现灰色背景

时间:2017-09-21 09:30:21

标签: javascript jquery django twitter-bootstrap

我有一个动态创建的表格,并且点击每一行显示一个灰色屏幕的视图按钮,但模型没有打开..我尝试了很多,但没有理解问题涉及的位置.I借助https://simpleisbetterthancomplex.com/tutorial/2016/11/15/how-to-implement-a-crud-using-ajax-and-json.html他的代码作为参考。

请帮助我......

m_manage.html ---这里我声明了模态并包含了页面

<div class="panel-body">
 <table class="table table-hover" id="table">                      
   <tbody>
      {% include 'm_manage/partial_m_manage.html' %}
   </tbody>
 </table>
</div>              
<div class="modal fade" id="modal-leave">
  <div class="modal-dialog">
   <div class="modal-content">
 </div>
</div>

partial_m_manage.html:它包含表格中的列

{% for  pl in pending_leaves %}
<tr> 
  <td><button type="button" class="btn btn-default btn-sm view" data-url="{% url 'm_manage:leave_view' pl.id %}" data-target="#{{pl.id}}_3"><i class="fa fa-eye" aria-hidden="true" style="color:black"></i></button>                                        
   </td>                                   
</tr>
{% endfor %}

js ----点击按钮

    $("#table").on('click', '.view', function (e) {
    var btn = $(this);
    console.log(btn);
    var url= btn.attr("data-url");
    console.log(url);
    $.ajax({
      // url: '/m_manage/leave_view/', 
      url: btn.attr("data-url"),
      dataType: 'json',
      type: 'get',

      beforeSend: function () {
       $("#modal-leave").modal("show");
      },
      success: function (data) {
        console.log('You Did it!!!!');
        $("#modal-leave .modal-content").html(data.html_form);
      }
    });
  });

views.py -

def leave_view(request,pk):
  Leave = get_object_or_404(leave, pk=pk);
  data=dict()
  if request.method == 'POST':
     data['html_form']= render_to_string('m_manage/view_modal.html',{'Leave':Leave}, request=request)
return JsonResponse(data)

view_modal.html ---我正在渲染一个新页面,即模态

<form method="post" action="{% url 'm_manage:leave_view' Leave.id %}" class="js-leave-view-form">
{% csrf_token %}
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">Do you want to accept {{Leave.id}} leave</h4>
  </div>
  <div class="modal-body"> 
  <h1>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</h1>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-primary">Create book</button>
  </div>
  </form>

但是出现黑屏并且所有值都在控制台中成功打印,但模态弹出窗口不会出现......

请帮助我...... :(

2 个答案:

答案 0 :(得分:0)

您应该包含“bootstrap.min.css”。

答案 1 :(得分:0)

我遇到了这个问题。原来是因为触发模态的按钮在被渲染的部分之前。我只是在 html 中切换了这两件事,即

<%= render 'some_modal' %>
<%= link_to 'Click here' %>

不是

<%= link_to 'Click here' %>
<%= render 'some_modal' %>