繁体   English   中英

如何在 ajax success 中打开 bootstrap modal

[英]How to open bootstrap modal in ajax success

我想通过 jquery 打开引导程序模式。我知道 ajax 运行成功,因为它会抛出警报。 但是无法打开模态。 这些是我的代码。

$.ajax({
    type: "POST",
    url: "<?php echo base_url() . 'index.php/application/requestCode'; ?>",
    data: {
        'apiName': apiName,
        'api': api,
        'hotel': hotel,
        'payment':payment,
        'template': template
    },
    success: function(msg)
    {
        $("#getCodeModal").modal("toggle");
        $("#getCode").html(msg);
    }
});

我的模式 HTML 是:

 <!-- Modal -->
 <div class="modal fade" id="getCodeModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog modal-lg">
      <div class="modal-content">
       <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" id="myModalLabel"> API CODE </h4>
       </div>
       <div class="modal-body" id="getCode" style="overflow-x: scroll;">
          //ajax success content here.
       </div>
    </div>
   </div>
 </div>

控制台错误:模态不是 function。

试试这个

success: function(resp){
    $("#getCode").html(resp);
    $("#getCodeModal").modal('show');
}

试试这个:

success: function(data) {
    $("#getCode").html(data);
    jQuery("#getCodeModal").modal('show');
}

这应该有效:)。

写下以下代码。

success: function(msg)
    {
        $("#getCodeModal").modal("show");
        $("#getCode").html(msg).show();
    }

当您两次包含库 jquery 时会发生这种情况。 检查一下,也许你将它包含在你的索引中,然后又不必要地在你的部分页面中。

第一次切换模态后,你必须再次切换它以“关闭它”.. 成功时 function 切换它,它会正常显示。 例子:

$('#Modal').modal('toggle)  //this is the first time to toggle the modal
$('#Modal').modal('toggle)  //this is the second time "to turn it off"
success:function(){
$('#Modal').modal('toggle)  //this is the third time to "turn it on again"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM