最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bootstrap data-toggle modal content within a function - Stack Overflow

programmeradmin2浏览0评论

I'm new to programming, and I have this simple question but I can't figure out how to do it.

I got this working code from Boostrap 4:

 <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
      Launch demo modal
    </button>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

But instead of having a button I want to create a function to fire up the modal content:

function openModal(){
  /* DESCRIPTION: Open the warning modal */
   // data-toggle="modal" data-target="#exampleModal"
}

But I don't know how to the same thing as data-toggle and data-target do in the button click.

Someone has a hint how to do that?

I'm new to programming, and I have this simple question but I can't figure out how to do it.

I got this working code from Boostrap 4:

 <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
      Launch demo modal
    </button>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

But instead of having a button I want to create a function to fire up the modal content:

function openModal(){
  /* DESCRIPTION: Open the warning modal */
   // data-toggle="modal" data-target="#exampleModal"
}

But I don't know how to the same thing as data-toggle and data-target do in the button click.

Someone has a hint how to do that?

Share Improve this question asked Dec 2, 2018 at 10:18 sibamssibams 1874 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You have to use the Bootstrap JS api for the modal ( Link attached at the end of this answer ).

Here is the straight answer :-)

function openModal(){
   /* DESCRIPTION: Open the warning modal */
   $('#exampleModalLong').modal('show');

}

Here is another SO question related to this : How can I trigger a Bootstrap modal programmatically?

Here is a detailed link on Bootstrap official documentation on various javascript methods for modals.

https://getbootstrap./docs/4.0/ponents/modal/#methods

Just use this line in your function:

$('#myModal').modal('show');

Note: remember to replace your modal id with myModal.

发布评论

评论列表(0)

  1. 暂无评论