引导模式加载事件

Bootstrap modal onload event

本文关键字:事件 加载 模式      更新时间:2023-09-26

引导模式是否有onload事件?我想在打开模态时调用一个http GET请求,用rest api中的数据填充模态。我在模态中有一个表单,并且在表单的onload事件上调用了GET函数,但它不起作用。

<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">Agent details:</h4>
      </div>
      <div class="modal-body">
<form onload="getData()" style= "font-size: 16pt">

根据文档,您可以收听显示或显示的事件。

来自文件:

show.bs.model=>调用show实例方法时,此事件会立即激发。

shown.bs.modal=>当模态对用户可见时(将等待CSS转换完成),会触发此事件。

$('#myModal').on('show.bs.modal', function () {
  // do something…
})

我发现最好的方法是将onclick属性添加到调用modal的html元素中。

<button type="button" onclick="PopulateAddModal()" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#AddModal">Add</button>

然后在您的javascript中。。

    function PopulateAddModal() {
      //do stuff here...
      }

这将确保您的代码在调用模态后立即运行。否则,在模态完全完成其"淡入"动画之前,javascript代码将不会运行。