简体   繁体   English

强制检查dirtyforms.js

[英]Force check dirtyforms.js

I'm trying to check for dirty form when swithing between tabs - and if the form is dirty, show the alert. 在标签之间切换时,我正在尝试检查表单是否脏-如果表单脏了,请显示警报。

I'm using this plugin: https://github.com/snikch/jquery.dirtyforms 我正在使用此插件: https : //github.com/snikch/jquery.dirtyforms

Ii works fine when trying to go to an external page (here i will get the warning), but when i switch between tabs(bootstrap), nothing happens. 尝试转到外部页面时,II正常工作(在这里我会得到警告),但是当我在选项卡(引导程序)之间切换时,什么也没发生。 I have made a speciale class(".chkChange") to listen to if the form is dirty, but nothing happens when I click on a tab. 我制作了一个特殊类(“ .chkChange”)来侦听表单是否脏了,但是当我单击选项卡时没有任何反应。 The tabs looks like this: 选项卡如下所示:

<li class="setup-conditions"><a data-toggle="tab" class="chkChange" href="#setup-conditions">Procedure</a></li>

And i'm able to check if the form is dirt or not with this snippet, but i need help to trigger the alert build in dirtyforms: 我可以使用此代码段检查表单是否脏污,但我需要帮助以脏表单触发警报构建:

$('#myTab li a').click(function () {
    if ($('form').dirtyForms('isDirty')) {
        //alert("Form is dirty");
    }
});

And like i said, if I put the same class on another (external) link, it will prompt if anything has been changed - bot not on the tabs. 就像我说的,如果我将同一个类放在另一个(外部)链接上,它将提示是否有任何更改-bot不在选项卡上。

In this case, you can customize the event binding to attach the click handler to your link. 在这种情况下,您可以自定义事件绑定,以将点击处理程序附加到链接。

$(document).bind('bind.dirtyforms', function (ev, events) {
    var originalBind = events.bind;

    events.bind = function (e) {
        $('#myTab li a').on('click', events.onAnchorClick);
        originalBind(e);
    };
});

Dirty Forms will then correctly 脏表格将正确无误

  1. Check whether the form is dirty 检查表格是否脏
  2. If dirty, prevent the click event 如果脏了,防止点击事件
  3. Show the dialog 显示对话框
  4. If the user decides to proceed, will refire the click event 如果用户决定继续,将重新触发click事件

Dirty Forms ignores your anchor tag by automatically because it has no HREF tag. 脏表单会自动忽略您的锚标记,因为它没有HREF标记。 This was a feature that was contributed by the community, that I am now reconsidering because apparently there is an argument to monitor anchor tags that don't have HREF sometimes. 这是由社区贡献的一项功能,我现在正在重新考虑,因为显然有一个参数来监视有时没有HREF的锚标签。

Update 更新

The default behavior has changed in 2.0.0-beta00005 to include links with no HREF tag by default. 默认行为在2.0.0-beta00005中已更改,默认情况下包括不带HREF标签的链接。 That should fix this so you don't need to attach the event. 这应该可以解决此问题,因此您无需附加事件。 However, depending on what libraries you are using, you may need to add an ignoreSelector to Dirty Forms to stop watching them. 但是,根据所使用的库,您可能需要向Dirty Forms中添加一个ignoreSelector以停止查看它们。

$('form').dirtyForms({ ignoreSelector: 'a.some-class:not([href])' });

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

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