diff --git a/core/misc/ajax.js b/core/misc/ajax.js index fefe9f3..7069e72 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -708,7 +708,7 @@ // when there is a form such that this.$form.ajaxSubmit() is used instead of // $.ajax(). When there is no form and $.ajax() is used, beforeSerialize() // isn't called, but don't rely on that: explicitly check this.$form. - if (this.$form) { + if (this.$form && $.contains(document, this.$form.get(0))) { var settings = this.settings || drupalSettings; Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize'); } @@ -957,8 +957,9 @@ $(this.wrapper).show(); // Re-enable the element. $(this.element).prop('disabled', false); - // Reattach behaviors, if they were detached in beforeSerialize(). - if (this.$form) { + // Reattach behaviors, if they were detached in beforeSerialize(), and the + // form is still part of the document. + if (this.$form && $.contains(document, this.$form.get(0))) { var settings = this.settings || drupalSettings; Drupal.attachBehaviors(this.$form.get(0), settings); }