]> source.dussan.org Git - jquery.git/commitdiff
Fix #7061. Lazy-attach to the genuine submit event for delegation.
authorDave Methvin <dave.methvin@gmail.com>
Thu, 22 Sep 2011 01:15:00 +0000 (21:15 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Thu, 22 Sep 2011 01:15:00 +0000 (21:15 -0400)
Avoids the problems that arise when we try to trigger submit behavior at clicky-time.

src/event.js

index 02c26dc23a236348dc18f86852f037ff8a201c3f..ffb818d968ad355ed78ec8cb5750bf75610724a1 100644 (file)
@@ -721,7 +721,7 @@ jQuery.each({
        };
 });
 
-// submit delegation
+// IE submit delegation
 if ( !jQuery.support.submitBubbles ) {
 
        jQuery.event.special.submit = {
@@ -731,23 +731,34 @@ if ( !jQuery.support.submitBubbles ) {
                                return false;
                        }
 
+                       // Lazy-add a submit handler when a descendant form may potentially be submitted
                        jQuery.event.add( this, "click._submit keypress._submit", function( e ) {
+                               // Node name check avoids a VML-related crash in IE (#9807)
                                var elem = e.target,
-                                       type = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.type : "";
-
-                               // Do the elem.form check after type to avoid VML-related crash in IE (#9807)
-                               if ( (e.type === "click" && (type === "submit" || type === "image") && elem.form) || 
-                                        (e.type === "keypress" && e.keyCode === 13 && (type === "text" || type === "password") && elem.form) ) {
-                                       simulate( "submit", this, e );
+                                       form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined;
+                               if ( form && !form._submit_attached ) {
+                                       jQuery.event.add( form, "submit._submit", function( event ) {
+                                               // Form was submitted, bubble the event up the tree
+                                               if ( this.parentNode ) {
+                                                       simulate( "submit", this.parentNode, event, true );
+                                               }
+                                       });
+                                       form._submit_attached = true;
                                }
                        });
+                       // return undefined since we don't need an event listener
                },
 
                teardown: function() {
+                       // Only need this for delegated form submit events
+                       if ( jQuery.nodeName( this, "form" ) ) {
+                               return false;
+                       }
+
+                       // Remove delegated handlers; cleanData eventually reaps submit handlers attached above
                        jQuery.event.remove( this, "._submit" );
                }
        };
-
 }
 
 // IE change delegation and checkbox/radio fix