jQuery 隐藏时的引导模式有多清晰

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31022950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:46:30  来源:igfitidea点击:

How clear bootstrap modal on hide

jquerytwitter-bootstrapbootstrap-modal

提问by adam78

How to can you clear the bootstrap modal on dismiss/hide/close?

如何在关闭/隐藏/关闭时清除引导模式?

I have the following Modal definition:

我有以下模态定义:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
   Add New Comment
</button>

Partial view which contains Modal

包含 Modal 的局部视图

@Html.Partial("_CreateComment", Model)


 // Partial view which contains modal

 <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
      @using (Ajax.BeginForm("AddComment", "Blog", new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "comments",
                OnSuccess = "$('#myModal').modal('hide');"

            }))
      {
        <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Add Comment</h4>
        </div>
        <div class="modal-body">
                @Html.ValidationSummary(true)
                @Html.HiddenFor(model => model.Blog.BlogID)

                <div class="form-group">
                    @Html.LabelFor(model => model.BlogComment.Comment)
                    @Html.TextAreaFor(model => model.BlogComment.Comment, 4, 104, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.BlogComment.Comment)
                </div>

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary">Save changes</button>
            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
        </div>
      }
  </div>
   </div>
</div>

And this is the javascript I am using to to clear the content:

这是我用来清除内容的 javascript:

$(function () {
    //clear modal cache, so that new content can be loaded
    $('body').on('hidden.bs.modal', '.modal', function () {
        $(this).removeData('bs.modal');

    });
});

If I dismiss the modal after having entered some content or upon submission the content in the form doesn't clear?

如果我在输入某些内容或提交后关闭模态,表单中的内容不清晰?

回答by adam78

this is the easiest fix:

这是最简单的修复:

$('#myModal').on('hidden.bs.modal', function () {
    $(this).find("input,textarea,select").val('').end();

});

回答by Guruprasad Rao

Use val('')based on input types present and use #myModalinstead of body

使用val('')基于当前输入的类型和使用#myModal的,而不是body

$('#myModal').on('hidden.bs.modal', function () {
        $('.modal-body').find('textarea,input').val('');
});

DEMO

演示

回答by Mahmoud-Abdelslam

This worked for me

这对我有用

$('body').on('hidden.bs.modal', '.modal', function () {
        $(".modal-content").empty();
      });