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

javascript - Determine which button opened a Bootstrap 3 modal - Stack Overflow

programmeradmin2浏览0评论

I have the following Bootstrap buttons:

<button type="button" class="btn btn-primary btn-lg" data-button="create" data-toggle="modal" data-target="#modal">Create</button>
<button type="button" class="btn btn-primary btn-lg" data-button="delete" data-toggle="modal" data-target="#modal">Delete</button>

and this is the modal:

<div class="modal fade" id="modal">
  <div class="modal-dialog">
    <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">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

How can I figure out which button triggered the modal to open?

Specifically, I need the data-button attribute.

I have the following Bootstrap buttons:

<button type="button" class="btn btn-primary btn-lg" data-button="create" data-toggle="modal" data-target="#modal">Create</button>
<button type="button" class="btn btn-primary btn-lg" data-button="delete" data-toggle="modal" data-target="#modal">Delete</button>

and this is the modal:

<div class="modal fade" id="modal">
  <div class="modal-dialog">
    <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">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

How can I figure out which button triggered the modal to open?

Specifically, I need the data-button attribute.

Share Improve this question edited Feb 5, 2015 at 3:50 Josh Crozier 241k56 gold badges400 silver badges313 bronze badges asked Feb 5, 2015 at 2:22 narayannarayan 931 silver badge6 bronze badges 2
  • 2 If you have a different question, please ask a new question. It's not appropriate to add additional questions to an existing question. I have rolled back your last edit. – Simon MᶜKenzie Commented Feb 5, 2015 at 2:42
  • 1 @SimonMᶜKenzie thanks. The answer below answered it. thanks for your help. – narayan Commented Feb 5, 2015 at 2:55
Add a comment  | 

1 Answer 1

Reset to default 24

You can listen to either of the show.bs.modal/shown.bs.modal events.

Within the function, the event has a relatedTarget property attached to it. You can use this to determine which button triggered the modal to open.

Bootstrap 3 - Modal events:

This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.

$('.modal').on('show.bs.modal', function (e) {
    var $trigger = $(e.relatedTarget);
});

Example Here

.. and if you want to access the data-button attribute, use:

$(e.relatedTarget).data('button');

Conversely, if you want to determine which button caused the modal to close, see this answer.

发布评论

评论列表(0)

  1. 暂无评论