bootstrap下Ajax结束后关闭模态框
    		       		warning:
    		            这篇文章距离上次修改已过440天,其中的内容可能已经有所变动。
    		        
        		                
                在Bootstrap中,你可以使用JavaScript来关闭模态框。以下是一个简单的例子,展示了如何在Ajax请求结束后关闭模态框:
HTML:
<!-- 模态框 -->
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>JavaScript (使用jQuery):
$.ajax({
  url: 'your-endpoint.php', // 替换为你的API或服务器端点
  type: 'POST', // 或 'GET',根据你的需求
  data: {
    // 你要发送的数据
  },
  success: function(response) {
    // 请求成功后的回调函数
    // 可以在这里操作DOM或关闭模态框
    $('#myModal').modal('hide');
  },
  error: function(xhr, status, error) {
    // 请求失败后的回调函数
    console.error("An error occurred: " + status + "\nError: " + error);
  },
  complete: function() {
    // 请求完成后的回调函数(无论成功或失败)
    console.log('The request is complete!');
  }
});在上面的JavaScript代码中,$('#myModal').modal('hide') 是关闭模态框的关键行。当Ajax请求成功完成后,模态框会自动隐藏。如果你使用的是原生JavaScript而非jQuery,你需要使用原生的DOM操作来隐藏模态框,例如通过改变元素的样式或者移除显示的类。
评论已关闭