From 45dfa3b0fc49718c4f00600d1e25a129601d08ee Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 16 Sep 2009 08:33:00 +0000 Subject: [PATCH] Probably not the optimal solution, but tests pass. --- src/event.js | 38 +++++++++++++++++++++++++++++++++++++- test/unit/event.js | 16 ++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/event.js b/src/event.js index 76b1dfb20..0f976a060 100644 --- a/src/event.js +++ b/src/event.js @@ -405,7 +405,7 @@ jQuery.event = { add: function( proxy, data, namespaces ) { jQuery.extend( proxy, data || {} ); proxy.guid += data.selector + data.live; - jQuery.event.add( this, data.live, liveHandler ); + jQuery.event.add( this, data.live, liveHandler, data ); }, remove: function( namespaces ) { @@ -467,6 +467,7 @@ jQuery.Event.prototype = { if ( !e ) { return; } + // if preventDefault exists run it on the original event if ( e.preventDefault ) { e.preventDefault(); @@ -533,6 +534,41 @@ jQuery.each({ }; }); +(function() { + + var event = jQuery.event, + special = event.special, + handle = event.handle; + + special.submit = { + setup: function(data, namespaces) { + if(data.selector) { + event.add(this, 'click.specialSubmit', function(e, eventData) { + if(jQuery(e.target).filter(":submit, :image").closest(data.selector).length) { + e.type = "submit"; + return handle.call( this, e, eventData ); + } + }); + + event.add(this, 'keypress.specialSubmit', function( e, eventData ) { + if(jQuery(e.target).filter(":text, :password").closest(data.selector).length) { + e.type = "submit"; + return handle.call( this, e, eventData ); + } + }); + } else { + return false; + } + }, + + remove: function(namespaces) { + event.remove(this, 'click.specialSubmit'); + event.remove(this, 'keypress.specialSubmit'); + } + }; + +})(); + // Create "bubbling" focus and blur events jQuery.each({ focus: "focusin", diff --git a/test/unit/event.js b/test/unit/event.js index 197406b66..d2da8e241 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -798,6 +798,22 @@ test(".live()/.die()", function() { jQuery('span#liveSpan1').die('click'); }); +test("live with submit", function() { + var count = 0; + + jQuery("#testForm").live("submit", function() { + count++; + return false; + }); + + jQuery("#testForm input[name=sub1]")[0].click(); + jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13}); + + equals(2, count); + + jQuery("#testForm").die("submit"); +}); + test("live with focus/blur", function(){ expect(2);