gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:18:54 27 4
gpt4 key购买 nike

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

为了关闭模式,我在按钮的属性中设置了 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');

最佳答案

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

 $('.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>

关于javascript - 如何使用 Javascript 更改模式中按钮的数据目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51520692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com