簡體   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