From 1fb2f92c357b985a5ba18d0938edd27b5f64d8c3 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 20 Nov 2012 22:31:33 -0500 Subject: [PATCH] Fix #3827. Get the correct checkbox status for a click handler. --- src/event.js | 12 ++++++++++-- test/unit/event.js | 29 +++++++++++++++++++++++++++++ test/unit/manipulation.js | 2 +- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/event.js b/src/event.js index c3da76752..aeee603f7 100644 --- a/src/event.js +++ b/src/event.js @@ -263,7 +263,7 @@ jQuery.event = { // Allow special events to draw outside the lines special = jQuery.event.special[ type ] || {}; - if ( special.trigger && special.trigger.apply( elem, data ) === false ) { + if ( !onlyHandlers && special.trigger && special.trigger.apply( elem, data ) === false ) { return; } @@ -523,7 +523,15 @@ jQuery.event = { // Prevent triggered image.load events from bubbling to window.load noBubble: true }, - + click: { + // For checkbox, fire native event so checked state will be right + trigger: function() { + if ( jQuery.nodeName( this, "input") && this.type === "checkbox" && this.click ) { + this.click(); + return false; + } + } + }, focus: { delegateType: "focusin" }, diff --git a/test/unit/event.js b/test/unit/event.js index db968284f..f2f3cddd4 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2831,6 +2831,35 @@ test("clone() delegated events (#11076)", function() { clone.remove(); }); +test("checkbox state (#3827)", function() { + expect( 9 ); + + var markup = jQuery("
").appendTo("#qunit-fixture"), + cb = markup.find("input")[0]; + + jQuery(cb).on( "click", function(){ + equal( this.checked, false, "just-clicked checkbox is not checked" ); + }); + markup.on( "click", function(){ + equal( cb.checked, false, "checkbox is not checked in bubbled event" ); + }); + + // Native click + cb.checked = true; + equal( cb.checked, true, "native - checkbox is initially checked" ); + cb.click(); + equal( cb.checked, false, "native - checkbox is no longer checked" ); + + // jQuery click + cb.checked = true; + equal( cb.checked, true, "jQuery - checkbox is initially checked" ); + jQuery( cb ).click(); + equal( cb.checked, false, "jQuery - checkbox is no longer checked" ); + + // Handlers only; checkbox state remains false + jQuery( cb ).triggerHandler( "click" ); +}); + test("fixHooks extensions", function() { expect( 2 ); diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index f08e7bd90..1d0dc4538 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -100,7 +100,7 @@ var testWrap = function(val) { ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); jQuery(checkbox).wrap(val( "" )); ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" ); - }).click(); + }).prop( "checked", false )[0].click(); // using contents will get comments regular, text, and comment nodes var j = jQuery("#nonnodes").contents(); -- 2.39.5