在Bootstrap模态中单击按钮以获取启动元素?

时间:2018-08-31 12:52:32

标签: javascript jquery bootstrap-4 bootstrap-modal

如何通过模态内部的按钮来获取最初按下以打开模态的按钮?

HTML:

<button id="opener" type="button" data-toggle="modal" data-target="#myModal">Open Modal</button>

<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <button type="button" id="targetModalOpener">Button</button>
            </div>
        </div>
    </div>
</div>

jQuery:

$('#targetModalOpener').closest('.modal')

我想通过按$('#opener')来获取$('#targetModalOpener')

出于这个问题的目的,我给了打开器一个id属性,尽管它没有一个。

2 个答案:

答案 0 :(得分:0)

您可以使用Bootstrap Modal Show Event并使用activeElement来获得打开模式的最初按下的按钮,在这种情况下,该按钮就是触发模式的按钮。

$(document).ready(function(){

var opener;

$('.modal').on('show.bs.modal', function(e) {
    opener = document.activeElement;
});

$('.modal button').click(function(){
    console.log(opener);
});

});
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<body>
<button id="openerABC" type="button" data-toggle="modal" data-target="#myModalABC">Open Modal ABC</button>

<button id="openerXYZ" type="button" data-toggle="modal" data-target="#myModalXYZ">Open Modal XYZ</button>

<div id="myModalABC" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" id="targetModalOpenerABC">Button</button>
      </div>
    </div>
  </div>
</div>

<div id="myModalXYZ" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" id="targetModalOpenerXYZ">Button</button>
      </div>
    </div>
  </div>
</div>

</body>

答案 1 :(得分:0)

根据模式events,您可以获取事件 relatedTarget 并将其保存为按钮的数据属性:

$('#myModal').on('show.bs.modal', function(e) {
    $('#targetModalOpener').data('id', e.relatedTarget);
});
$('#targetModalOpener').on('click', function(e) {
    var opener = $(this).data('id');
    console.log(opener.outerHTML);
})

$('#myModal').on('show.bs.modal', function(e) {
    $('#targetModalOpener').data('id', e.relatedTarget);
});
$('#targetModalOpener').on('click', function(e) {
    var opener = $(this).data('id');
    console.log(opener.outerHTML);
    
    
    $('#myModal').modal('hide')
})
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<button id="opener" type="button" data-toggle="modal" data-target="#myModal">Open Modal1</button>
<button id="opener2" type="button" data-toggle="modal" data-target="#myModal">Open Modal2</button>

<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <button type="button" id="targetModalOpener">Button</button>
            </div>
        </div>
    </div>
</div>