jQuery .click() and the double submit of a form
jQuery is one of the greatest JavaScript frameworks with plenty of documentation for just about everything one can do with it. In my latest project I have used jQuery extensively for any UX aspect: form validations, dialogs, error messages, AJAX, UI, etc.
A feature that I would really like to see implemented on all HTML forms is the ability to submit the form by using the Return key as well. I am sure that there are a lot of users who really like to keep their hands on the keyboard as much as possible, without using the mouse just to click a button or without hitting the TAB key in order to focus on the submit button after which to press Return. Some of the particularities of my project are the fact that it uses Struts2 and the Struts2 jQuery plugin. This plugin allows developers either to use the .jar embedded JavaScript libraries or to automatically get the required files from Google’s servers (CDN). In July, when my project was using Oracle 11G and TopLink everything worked okay. Now, after I have migrated the project to open-source solutions (MySQL and EclipseLink) I have noticed that whenever I tried to create a new account into the application I would get 2 submits for one action. At first I thought that this had something to do with EclipseLink. After some debugging I actually found out that it was a JavaScript issue (it is possible that now I’m using another jQuery UI release).
The problem
The code responsible for the issue is the following:
$('#newProfileForm input:submit').button().click(function(e) {
if(checkInputs()) {
$('#newProfileForm form').submit();
}
});
$('#newProfileForm input').keyup(function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
if(checkInputs()) {
$('#newProfileForm').submit();
}
}
});
What I have discovered is that the .click() handler actually binds itself to keyboard events too, not only mouse events. This means that if the user presses the Return button the event will be identified with a click event.
The solution
Even though if the behaviour mentioned previously is unexpected (who would have thought that a click event would be triggered by the keyboard even if the focus is not set on the submit button?!), there are two ways of avoiding it in the code above:
- eliminate the
.keyup()handler because pressing on the Return button will be considered a click event; - alter the first bind for the
.click()handler like this:$('#newProfileForm input:submit').button().click(function(e) { e.preventDefault(); // stop the browser's default behaviour if(checkInputs()) { $('#newProfileForm form').submit(); } });
Have you ever had similar problems?
You might also like:
3 Comments
Leave a comment
Recent Posts
Projects that I support
Recent Comments
nope said:
yeah that was my first thought too, but: mount: warning: seems to be mounted read-write. too bad, would have been just perfect. more»Klaus Deiss said:
Dear Radu, I tried it on Ubuntu 10.0.4.2 and 10.0.4.3 with different kernel versions (amd64 server 2.6.32 kernel). No... more»scompo said:
Nope.. Now it’s not working again.. This printer it’s a real pain in the butt.. The other hp printer I had... more»Dmitrij said:
Thank you Peter and Patrice. Could you please post the updated script? more»hd_flash_pains said:
didn’t work for me more»








Y’know, there’s a nicer framework to do what you seem to need: qooxdoo. jQuery is for making web pages, qooxdoo is for making web apps. Besides abstracting all browser differences for supported browsers, regarding DOM structure, CSS, layout model etc., qooxdoo is much more what a programmer is used to, and doesn’t require a single bit of HTML, as opposed to jQuery, where you need to create the web pages as static HTML, manually take care of browser differences, style your pages using CSS, then script various elements in the pages to make them act like an application.
Other than that, it seems to me qooxdoo has one of the largest widget libraries among similar libraries, promotes a purely object oriented programming model, and also abstracts away such things as Xhtml calls to the server. It is also tested for memory leaks in IE, which other frameworks (notably ext) aren’t.
What you should have done is to drop the click events altogether and use the submit event of form elements. That would have taken care of both click and enter submits:
$("#id-of-form-element").submit(function () { return checkInputs(); });Unfortunately I couldn’t use what you proposed because I did not want to allow the user to submit forms if JavaScript was disabled in his browser. Therefore I assumed that the browser won’t produce the “submit” event.