我在引导模态中有一个列表框和一个按钮。单击按钮后,新的按钮将在模态的div内渲染。当我关闭模态并重新打开它时,模态上执行的最后一个操作(如先前呈现的按钮)仍然存在于模态中。
如何重置模态,以便再次打开模态时,按钮不存在,用户可以再次从列表框中选择选项,然后单击按钮以呈现新按钮,依此类推。
<!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title" id="myModalLabel">Select Language</h4> </div> <div class="modal-body"> <button type="button" class="btn" data-dismiss="modal">Close</button> <button type="button" class="btn" id="submit_form">Submit</button> <div class="modal-body1"> <div id="placeholder-div1"> </div> </div> </div> <div class="modal-footer"> <script> $('#submit_form').on('click', function() { $(".modal-body1").html('<h3>test</h3>'); }); </script> <script> $(function(){ $('.modal-footer').click(function() { $('.modal').modal('hide'); }); }); </script> </div> </div> </div> </div>
-更新-
为什么不起作用?
<script type="text/javascript"> $(function(){ $('#myModal').on('hidden.bs.modal', function (e) { console.log("Modal hidden"); $("#placeholder-div1").html(""); }); }); </script>
隐藏模态后,只需手动重置任何内容即可:
$(".modal").on("hidden.bs.modal", function(){ $(".modal-body1").html(""); }); $(document).ready(function() { $(".modal").on("hidden.bs.modal", function() { $(".modal-body1").html("Where did he go?!?!?!"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch modal </button> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span> </button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> <div class='modal-body1'> <h3>Close and open, I will be gone!</h3> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div>