简体   繁体   English

在Boostrap模态关闭事件上重置表单

[英]Reset Form on Boostrap modal close event

So i have a data table that you can select a row and it gets the data from the table and puts it into a form thats in a modal all of that works but when i close the modal i wanna reset the form. 所以我有一个数据表,你可以选择一行,它从表中获取数据,并将其放入一个模式中的形式,所有这些工作,但当我关闭模态我想重置表格。

$('.modal').on('hide.bs.modal', function () {
    // reset form
    $('#user_form').trigger('reset');
});

but when i close the modal the form isnt reset. 但当我关闭模态时,表单不会重置。

demo - http://codepen.io/anon/pen/VjggYo 演示 - http://codepen.io/anon/pen/VjggYo

I think you do not need shown.bs.modal event to show data in modal. 我认为你不需要shown.bs.modal事件来显示模态中的数据。

Pleas try after removing this event. 在删除此事件后请尝试。

$('.modal').on('shown.bs.modal', function () { }

And edit this : 并编辑:

$(this).find('input').each(function (index) {

To this: 对此:

$('.modal').find('input').each(function (index) {

Why do you need to reset the form? 为什么需要重置表单? If the form is populated each time a row is selected, then the form in the modal will always show the relevant data from the selected row. 如果每次选择行时都填充表单,则模态中的表单将始终显示所选行的相关数据。

The way I deal with these situations is to use AngularsJS's data binding to manage data in forms. 我处理这些情况的方法是使用AngularsJS的数据绑定来管理表单中的数据。 In order to reset a form, you simply reset the associated model in the Angular Controller. 要重置表单,只需在Angular Controller中重置关联的模型即可。

$scope.myModel = {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM