如何使用Javascript更改模式中按钮的数据目标?

时间:2018-07-25 13:53:56

标签: javascript jquery bootstrap-4 bootstrap-modal

我在模态中有一个按钮。当我按下按钮时,我希望该模式关闭,而另一个模式打开。但是,每次打开的模式都不相同,因此每次按下按钮时的数据目标都需要更改。

为了关闭模式,我在按钮的属性中添加了data-dismiss="modal"

我有三个ID为1,2,3的模态。我想使用随机Javascript函数选择打开哪个

var randomnumber = num.toString(Math.floor(Math.random() * (3 - 1 + 1)) + 1);

我尝试这样做,但是没有用:

$('#button').click(function() {
$(randomnumber).modal('show');

1 个答案:

答案 0 :(得分:1)

更新:此代码,每次都会打开一个随机模态并关闭另一个模态。

 $('.fade').hide();
$('.btn-info').click(function()
{
var randomnumber =Math.floor(Math.random() * 3) + 1;
  $('.fade').hide();
  $('#m'+randomnumber).show(10);
  alert("modal" + randomnumber)
});


$('#btn3').click(function()
{
   $('.fade').hide();
  $('#myModal').show();
 
});

$('.close').click(function()
{
   $('.fade').hide();

});
.fade
{
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button"  class="btn btn-info2 btn-lg" id="btn3" data-toggle="modal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;      </button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <button type="button" class="btn btn-info" id = "random" >Random Modal</button>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default close" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>


<div class="modal fade" id="m1" role="dialog" data-dismiss="modal">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;      </button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <button type="button" class="btn btn-info" id = "random">Random Modal</button>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default close" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
 <div class="modal fade" id="m2" role="dialog" data-dismiss="modal">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close">&times;      </button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <button type="button" class="btn btn-info" id = "random">Random Modal</button>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default close" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
<div class="modal fade" id="m3" role="dialog" data-dismiss="modal">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;      </button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <button type="button" class="btn btn-info" id = "random">Random Modal</button>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default close" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>