views:

868

answers:

3

I have bound a JavaScript function to the submit button of a form, causing the form to be submitted via an xhrPost request (using Dojo). However, when the user hits "Enter" in Safari, the form is submitted the usual way.

Is there any way to prevent Safari from submitting the form when the Enter key is pressed? Can I somehow bind my JavaScript function to the enter key instead?

Thanks a lot in advance!

--Andreas

A: 

This is probably happening due to an error in the submission handler code - Safari will submit the form as usual if there is an exception there. Check to see if the Safari console is showing any errors.

I also had a similar issue once when the <input> elements were outside of the <form> tag. Double check that, too, perhaps.

Jesse Rusak
A: 

How I solved this problem was to disable the 'Enter' key. Not very elegant, and still looking for a better approach.

$('form').keypress(function (e){ if((e.which==13){ return false; }});

Christopher Altman
+1  A: 

Are you returning the false value from from the onsubmit handler? That's all that should be required to prevent normal form submission (provided Javascript is enabled).

David Toso