如何根据按钮单击更改 Bootstrap 模态内容

How to change Bootstrap modal content depending on button click

我需要能够根据单击的按钮更改模式内的内容。

例如,如果单击第一个按钮,它只会显示 divclass 'one',其他的将保持隐藏状态。

$('#exampleModalCenter').modal('toggle')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="btn" data-toggle="modal" data-target="#exampleModalCenter">one</button>

<button type="button" class="btn" data-toggle="modal" data-target="#exampleModalCenter">two</button>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<!-- jQuery library -->


<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>


<button type="button" class="btn" data-toggle="modal" data-target="#exampleModalCenter">three</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <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>
      </div>
      <div class="modal-body">
        <div class="one">Button one content</div>
        <div class="two">Button two content</div>
        <div class="one">Button three content</div>
      </div>
    </div>
  </div>
</div>

你可以看看这个教程: https://getbootstrap.com/docs/3.4/javascript/#modals-related-target

只需将数据属性添加到您的按钮即可:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#yourModal" data-name="one">Open modal for One</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#yourModal" data-name="two">Open modal for Two</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#yourModal" data-name="three">Open modal for Three</button>

并且当事件发生时,您可以恢复按钮的属性并根据它调整行为:

$('#yourModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  var recipient = button.data('name'); // Extract info from data-* attributes

  // Here, you can change the behavior according to the button clicked :
  switch(recipient) {
    case 'one':
      $('.one').show();
      break;
    case 'two':
      $('.two').show();
       break;
    // etc...
  }
});

您可以订阅模态的 show.bs.modal 事件,检查事件的 relatedTarget 属性 的值,然后根据该目标值检查 show/hide 内部 div

示例如下。逻辑:当你点击一个按钮时,先隐藏所有内容,然后根据你点击的按钮显示具体内容。

参考:https://getbootstrap.com/docs/3.3/javascript/#modals-related-target

$(".btn").click(function () {
  $(".parent").children().each(function () {
    $(this).hide();
  });
  $(`div.${$(this).attr("id")}`).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link
  rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
/>

<!-- jQuery library -->

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<button
  id="one"
  type="button"
  class="btn"
  data-toggle="modal"
  data-target="#exampleModalCenter"
>
  one
</button>

<button
id="two"
  type="button"
  class="btn"
  data-toggle="modal"
  data-target="#exampleModalCenter"
>
  two
</button>

<button
id="three"
  type="button"
  class="btn"
  data-toggle="modal"
  data-target="#exampleModalCenter"
>
  three
</button>

<!-- Modal -->
<div
  class="modal fade"
  id="exampleModalCenter"
  tabindex="-1"
  role="dialog"
  aria-labelledby="exampleModalCenterTitle"
  aria-hidden="true"
>
  <div class="modal-dialog modal-dialog-centered" role="document">
    <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>
      </div>
      <div class="modal-body">
        <div class="parent">
          <div class="one">Button one content</div>
          <div class="two">Button two content</div>
          <div class="three">Button three content</div>
        </div>
      </div>
    </div>
  </div>
</div>

 

   $(document).on("click",".modal-open",function(){
        var button_val = $(this).attr("data-val");

        $('#exampleModalCenter').modal('show');
        $(".conditional-div").hide();
        $("#exampleModalCenter ."+button_val).show();
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="btn modal-open" data-val="one" >one</button>
<button type="button" class="btn modal-open" data-val="two" >two</button>
<button type="button" class="btn modal-open" data-val="three" >three</button>

   
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<!-- jQuery library -->


<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>


  

<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <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>
      </div>
      <div class="modal-body">
        <div class="one conditional-div">Button one content</div>
        <div class="two conditional-div">Button two content</div>
        <div class="three conditional-div">Button three content</div>
      </div>
    </div>
  </div>
</div>

你可以这样做:

$('button.btn-open-modal').on('click', function(){
    let targetClass = $(this).attr("target-class");
    $("#exampleModalCenter div.modal-body > div").each(function(){
        if($(this).hasClass(targetClass)){
            $(this).show();
        }else {
            $(this).hide();
        }
    });
    $('#exampleModalCenter').modal('toggle');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<!-- jQuery library -->


<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<button type="button" class="btn btn-open-modal" target-class="one">one</button>
<button type="button" class="btn btn-open-modal" target-class="two">two</button>
<button type="button" class="btn btn-open-modal" target-class="three">three</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <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>
      </div>
      <div class="modal-body">
        <div class="one">Button one content</div>
        <div class="two">Button two content</div>
        <div class="three">Button three content</div>
      </div>
    </div>
  </div>
</div>