如何在 ajax 成功中打开引导模式

How to open bootstrap modal in ajax success

本文关键字:模式 ajax 成功      更新时间:2023-09-26

我想通过jquery打开bootstrap mod。我知道 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>

控制台中的错误:模态不是一个函数。

试试这个

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 两次时,就会发生这种情况。 检查一下,也许您将其包含在索引中,然后再次不必要地包含在部分页面中。

第一次切换模态后,您必须再次将其切换为"关闭"..并在成功功能上切换它,它将正常显示。例:

$('#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"
}