Bug Tracker

Modify

Ticket #6337 (closed bug: fixed)

Opened 4 years ago

Last modified 4 years ago

Button click within form causes submit event even though return false is there

Reported by: margieroginski Owned by: dmethvin
Priority: undecided Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: submit event return false Cc:
Blocking: Blocked by:

Description

I have a small test case below where I create a button inside a form and set its onclick hander to my js function, show_input(), followed by a "return false", like this:

Immediately following that is a one line script that clicks the button.

	    <button id="testbutton1" onclick="show_input(); return false;">
	       Test
	    </button>
	    <script type="text/javascript">
              $('#testbutton1').click();
	    </script>

In jquery 1.3.2, when the .click() occurs, it calls show_input() and no submit event occurs aftwards, due to the return false. But in in Jquery 1.4.2, it calls show_input() and then the submit event fires (it does a GET), as if the return false is not there.

If I manually click the button, I do not see the submit event, so this seems to be somehow related to the click being inline.

Here is the exact html that can be used to replicate it.

<html>
  <head>
    <script src="/site_media/js/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
      function show_input() {
        console.log("hello, world");
      }
    </script>
  </head>

  <body>
  <div id="container">
      <form>
      <div class="taskform_row_description form-row">
	    <button id="testbutton1" onclick="show_input(); return false;">
	       Test
	    </button>
	    <script type="text/javascript">
              $('#testbutton1').click();
	    </script>
      </div>
      </form>
  </div>
  </body>
</html>

This seems to me to be a bug since it is very incompatible with previous behavior...

Thanks, Margie

Change History

comment:1 Changed 4 years ago by dmethvin

  • Component changed from unfiled to event

comment:2 Changed 4 years ago by dmethvin

  • Owner set to dmethvin

It looks like the problem is in jQuery.event.trigger; the code to run inline scripts sets event.result to false but does not stopPropagation or preventDefault. I've taken the ticket and will put together a patch.

comment:3 Changed 4 years ago by dmethvin

  • need changed from Review to Commit

comment:4 Changed 4 years ago by john

  • Priority set to undecided
  • Status changed from new to closed
  • Resolution set to fixed

Fixed  http://github.com/jquery/jquery/commit/adff8e45e255409e8f78a3347dea01bdbee0a53d

We only need to call preventDefault in this case.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.