当我使用数据打开模态并关闭并再次打开时,表单不会重置并显示旧数据 我该如何修复它并初始化表单数据?
我使用bootstrap v3.3.7和adminlte v2
模式HTML:
<div class="modal fade" id="new" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<img src="/image/loading.gif" class="loading"/>
<span>Lodaing . . .</span>
</div>
</div>
</div>
</div>
点击按钮
<button href="/edit/id" data-target="#new" data-toggle="modal" type="button" class="btn btn-success">Edit</button>
使用 spring boot cotroller打开模式内容:
@RequestMapping(value = "/edit")
public String edit(Model model,
@ModelAttribute(value = "id") String paramId){
if (paramId == 0){
//open modal without data...
}
if (paramId != 0){
//open modal with data...
}
model.addAttribute("edit", data);
return "view/edit"; //open modal html template
}
答案 0 :(得分:0)
很明显,每次打开和关闭模态时,模态内容都会保留,您应该使用以下代码清除模态内容:
$(document).on('hidden.bs.modal', '.modal', function (e) {
$(this).find(".modal-content").empty();
$(this).removeData('bs.modal');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<button href="/edit/id" data-target="#new" data-toggle="modal" type="button" class="btn btn-success">Edit</button>
<div class="modal fade" id="new" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<img src="/image/loading.gif" class="loading"/>
<span>Lodaing . . .</span>
</div>
</div>
</div>
</div>
答案 1 :(得分:-1)
将此内容放入您页面的javascript中,以在关闭时清除模式内容:
$(document).on('hidden.bs.modal', '.modal', function (e) {
// Handles the event thrown when a modal is hidden
$(this).removeData('bs.modal');
$(this).find(".modal-content").empty();
});