#4930 closed bug (wontfix)
Inline "onsubmit" handler is not called when "submit" event is triggered
Reported by: | nbelaevski | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.4 |
Component: | unfiled | Version: | 1.3.2 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Triggering "submit" event on FORM element submits form, but doesn't check bound "onsubmit" handler. Only callbacks attached by jQuery.bind() are called.
Attached test case triggers "click" and "submit" events for comparison.
Attachments (1)
Change History (7)
Changed 14 years ago by
Attachment: | onsubmit-jquery-issue.html added |
---|
comment:1 follow-up: 2 Changed 14 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 Changed 13 years ago by
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Replying to dmethvin:
However, this is a browser quirk and outside the scope of jQuery. If you want jQuery to execute an event on submit, define the event with jQuery.
I do not agree - the documentation implies that this should work and there IS code in jQuery to handle triggering onevent-functions - it just does not cover onsubmit yet:
In jQuery 1.3.2 it's the following line:
(jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false ) |
event.result = false;
In subversion jQuery we have in events.js line 256 following: Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
if ( (!nativeFn (jQuery.nodeName(elem, 'a') && type === "click")) && nativeHandler && nativeHandler.apply( elem, data ) === false ) {
type === "submit" case could be added here for improved API consistency. |
comment:3 Changed 13 years ago by
Sorry for the broken code excerpts before:
jquery 1.3.2:
if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false ) event.result = false;
jQuery Subversion:
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links) if ( (!nativeFn || (jQuery.nodeName(elem, 'a') && type === "click")) && nativeHandler && nativeHandler.apply( elem, data ) === false ) { event.result = false; }
comment:4 Changed 13 years ago by
Resolution: | → wontfix |
---|---|
Status: | reopened → closed |
.submit() doesn't appear to trigger the native onsubmit. Unfortunately we can't not execute the .submit() (like we can with the a click) so I guess we'll just have to bite the bullet on this one. Sorry!
comment:5 follow-up: 6 Changed 11 years ago by
This seems to have changed, I switched from jquery 1.4.2 to 1.7.1, and now the onsubmit is called by jquery. I would like to know since what version and why.
comment:6 Changed 11 years ago by
Replying to [email protected]…:
This seems to have changed, I switched from jquery 1.4.2 to 1.7.1, and now the onsubmit is called by jquery. I would like to know since what version and why.
I also noticed this behavior has changed when I switched from 1.3.2 to 1.7.1. So I made a little test to find out since what version this has been changed. Strangely enough I came to the conclusion this has been changed in version 1.4.1. I used the following html to find out:
<html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#myform").submit(); }); </script> </head> <body> <form id="myform" onsubmit="alert('onsubmit')" action=""> <input type="submit" /> </form> </body> </html>
1.4 is the latest version in which the alert box isn't shown. In 1.4.1 and later it is.
A form has both a submit() method and an onsubmit event. Unlike many/most other elements, browsers do not fire the element's onsubmit event when the submit() method is called. When you use
jQuery().trigger("submit")
jQuery is calling the native submit() method for you but since it doesn't "see" the native event and the native submit() doesn't trigger the native onsubmit, it is not called. However, this is a browser quirk and outside the scope of jQuery. If you want jQuery to execute an event on submit, define the event with jQuery.