无法通过Ajax加载Bootstrap Modal

时间:2014-02-28 09:09:14

标签: php jquery ajax codeigniter twitter-bootstrap

我的html代码为:

<button class="btn btn-default" id="openForm">
        New           
</button>

<!-- Modal -->
<div class="modal fade hide" id="formModal">
  <div class="modal-header">
    <h4>
        Form
    </h4>
  </div>

  <div class="modal-body" id="formModalBody">
  </div>

  <div class="modal-footer">
    <a href="#" data-dismiss="modal" class="btn">Close</a>        
  </div>
</div>

我的Java脚本读作:

$(document).on('click', '#openForm', function(e) {
e.preventDefault();

$("#formModalBody").html("");
$("#formModal").modal('show');

$.post('index.php/customers/view/-1/',
        {id: -1},
        function (html) {
            $("#formModalBody").html(html);
        }
    );
});

我正在使用Twitter Bootstrap&amp; Codeignitor。上面的帖子是index.php / Controller / function

当我点击按钮时;我无法看到Bootstrap模态。页面淡入,但没有模态可见。

2 个答案:

答案 0 :(得分:1)

  

这是for bootstrap 3,可以单独使用html触发,而不运行javascript                              启动演示模式       

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body" id="formModalBody">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<!-- For javascript -->
$.post('index.php/customers/view/-1/',
        {id: -1},
        function (html) {
            $("#formModalBody").html(html);
        }
);

答案 1 :(得分:0)

您应该使用anchor链接而不是按钮,它将使用默认设置

 <a href="/model_link" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Launch demo modal
    </a>

href链接响应代码将附加在

<div class="modal-body" id="formModalBody">
<!-- modal body -->
</div>