如何在关闭时重置bootstrap模式并再次打开它?

New*_*ils 53 javascript jquery html5 twitter-bootstrap

我在bootstrap模式中有一个列表框和一个按钮.单击该按钮时,将在模式中的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">&times;</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>
Run Code Online (Sandbox Code Playgroud)

- 更新---

为什么这不起作用?

              <script type="text/javascript">  
                $(function(){  
                      $('#myModal').on('hidden.bs.modal', function (e) {
                      console.log("Modal hidden");
                      $("#placeholder-div1").html("");
                    });
                });

              </script>
Run Code Online (Sandbox Code Playgroud)

Jus*_*nas 47

只需在隐藏模态时手动重置任何内容:

$(".modal").on("hidden.bs.modal", function(){
    $(".modal-body1").html("");
});
Run Code Online (Sandbox Code Playgroud)

还有更多的活动.更多关于他们的信息

$(document).ready(function() {
  $(".modal").on("hidden.bs.modal", function() {
    $(".modal-body1").html("Where did he go?!?!?!");
  });
});
Run Code Online (Sandbox Code Playgroud)
<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">&times;</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>
Run Code Online (Sandbox Code Playgroud)

  • 很抱歉我说错了@Justinas。当您有远程模式时,此方法可以完美工作,因为它会清除远程内容。在下一个请求时,它将从远程内容重新加载“.modal-body”。如果您有一个内联模式清除“.modal-body”,那么这不是一个好主意。我对内联模态所做的操作是使用切换方法,并在“.modal-body”内使用“隐藏”类。例如,我有 `&lt;div class="loading hide"&gt;&lt;span class="caption"&gt;正在加载...&lt;/span&gt;&lt;img src="/images/loading.gif" alt="loading"&gt;&lt; /div&gt;`,然后是`&lt;div class="context hide"&gt;实际内容&lt;/div&gt;`。顺便说一句,已投票 (2认同)
  • @Justinas然后,每次`调用modal.show()`时都需要重新填充body,我不能接受这种方法。 (2认同)

ehe*_*enr 24

对我有帮助的是将以下行放在ready函数中:

$(document).ready(function()
{
    ..
    ...

    // codes works on all bootstrap modal windows in application
    $('.modal').on('hidden.bs.modal', function(e)
    { 
        $(this).removeData();
    }) ;

    ...
    ..
});
Run Code Online (Sandbox Code Playgroud)

关闭模态窗口并再次打开时,先前输入和选择的值将重置为初始值.

我希望这对你也有帮助!


Rob*_*Rob 17

尝试克隆,然后在隐藏模态时删除并重新添加元素.这应该保留所有事件,尽管我还没有对所有版本的所有版本进行全面测试.

var originalModal = $('#myModal').clone();
$(document).on('#myModal', 'hidden.bs.modal', function () {
    $('#myModal').remove();
    var myClone = originalModal.clone();
    $('body').append(myClone);
});
Run Code Online (Sandbox Code Playgroud)


小智 13

试过了并且运作良好

$('#MyModal').on('hidden.bs.modal', function () {
    $(this).find('form').trigger('reset');
})
Run Code Online (Sandbox Code Playgroud)

reset 是dom内置功能,您也可以使用 $(this).find('form')[0].reset();

Bootstrap的模态类公开了一些事件,可以挂接到模态功能上,请参见此处

hide.bs.modal 调用hide实例方法后,立即触发此事件。

hidden.bs.modal 当模态完成向用户隐藏时将触发此事件(将等待CSS转换完成)​​。

  • 这是正确的答案。其他解决方案,例如添加/删除模式,或者弄乱模式的 html 内容,都只是 hack。 (3认同)

小智 9

要重置模态中的所有输入字段,请使用以下命令.

$(".modal-body input").val("")
Run Code Online (Sandbox Code Playgroud)