使用 jQuery 返回除 Drupal7 表单之外的所有点击事件的消息

Use jQuery to return a message for all click events except save for Drupal7 form

本文关键字:消息 事件 返回 jQuery Drupal7 表单 使用      更新时间:2023-09-26

我似乎无法弄清楚为什么当我单击"保存"时会显示我的消息,尽管我已经编写了逻辑,所以它不会。单击事件返回的类名是"表单提交",逻辑运算符有效...

Drupal.behaviors.saveOrExit = {
    attach: function (context, settings){
        p = location.pathname;
        if (p == '/user/1248/edit/configuration_1'){
            $(document).click(function(event) {
                if(event.target.className !== 'form-submit'){
                         $(window).trigger("unload");
                        window.onbeforeunload = function () { 
                        return "Your changes have not been saved! Do you really want to leave?" 
                    }
                }
            });
        };
    }
};

非常感谢任何帮助!

谢谢!

只是一个想法,这可能是你引用类名的方式

也许将元素类名称为

if($(this).attr("class") !== 'form-submit')

还要检查类名和"表单提交"是否属于同一类型,否则使用 != 而不是 !==

好的,显然这是一个相当普遍的问题,但我在 SO 上没有找到任何对我有用的东西。这种方法(感谢Max Morrow!)效果很好。

http://maxmorrow.wordpress.com/2012/08/08/jquery-prevent-a-user-from-leaving-a-page-with-unsaved-data/#comment-305