简体   繁体   English

打开和关闭模态后,Dirtyforms 停止工作

[英]Dirtyforms stop working after I open and close a modal

I have a form where I want to warn the user to save their data.我有一个表单,我想警告用户保存他们的数据。 it is done with Dirtyforms.它是用 Dirtyforms 完成的。

I have a button in my form, that opens up a modal dialog with some extra form.我的表单中有一个按钮,它打开一个带有一些额外表单的模式对话框。 So,所以,
1. I open my form dialog 1. 我打开我的表单对话框
2. edit (or not) my new form in the dialog 2. 在对话框中编辑(或不编辑)我的新表单
3. close the dialog 3.关闭对话框
4. return to my first form and click away. 4. 返回我的第一个表单并单击离开。 The dirtyforms not warns me about losing my data. dirtyforms 不会警告我丢失数据。

I checked it in debug mode, I see in my console this:我在调试模式下检查过它,我在控制台中看到:

[DirtyForms] Adding form w0 to forms to watch
[DirtyForms] Storing original value for _csrf
[DirtyForms] Storing original value for Prenajom[r1]
[DirtyForms] Entering: Leaving Event fired, type: click, element: http://k.local/..., class: btn btn-default popup-addon-table and id:
[DirtyForms] Leaving: Not dirty
[DirtyForms] Clearing the beforeunload event

At opening a modal, it fires the leaving event.在打开模态时,它会触发离开事件。 How can i prevent it?我该如何预防?

Thanks!谢谢!

By default, all anchor tags ( <a> ) get an event attached to them that fires the dialog because there is no reasonable way to differentiate between an anchor that leaves the page and one that does not.默认情况下,所有锚标记 ( <a> ) 都会附加一个触发对话框的事件,因为没有合理的方法来区分离开页面的锚和不离开页面的锚。 If you have anchor tags that do not navigate away, you should ignore them so they don't fire the dialog.如果您有无法导航的锚标记,则应忽略它们,以免它们触发对话框。

Option 1选项1

Set the ignoreSelector option to ignore specific fields, anchors, or buttons.设置 ignoreSelector 选项以忽略特定字段、锚点或按钮。

$('form').dirtyForms({ ignoreSelector: 'a.ignore-me' });

Option 2选项 2

Alternatively, add the value of $.DirtyForms.ignoreClass to any elements you wish to ignore, and Dirty Forms will ignore them.或者,将$.DirtyForms.ignoreClass的值添加到您希望忽略的任何元素,Dirty Forms 将忽略它们。

$('#ignored-element').addClass($.DirtyForms.ignoreClass);

Option 3选项 3

If you want to ignore more than one element at a time, you can add the value of $.DirtyForms.ignoreClass (with the default value dirtyignore ) to a containing element.如果你想一次忽略多个元素,你可以将$.DirtyForms.ignoreClass的值(默认值dirtyignore )添加到一个包含元素中。

<div class="dirtyignore">

    <!-- Everything here will be ignored - anchor, input, textarea, and select -->

</div>

I have received an answer from @NightOwl888, that solved my problem:我收到了@NightOwl888 的回答,解决了我的问题:

"One thing wrong with your setup is that Dirty Forms should always be the last jQuery plugin in the stack as noted in the docs. If you are attaching an event to the button after initializing Dirty Forms, it might not be ignoring correctly." “您的设置有一个问题,如文档中所述,Dirty Forms 应该始终是堆栈中的最后一个 jQuery 插件。如果您在初始化 Dirty Forms 后将事件附加到按钮,它可能没有正确忽略。”

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

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