views:

142

answers:

3

I'm using anchors as links, and also I'm binding the enter/return key to submit a form, like this:

$("form[name!=smsform]").bind("keyup", function(e){
if(e.keyCode == 13){
$(this).submit();
});

$("a[title=submit]").click( function(){
$(this).parents("form").submit();
});

However the form submits twice when clicking enter/return using the code above, I need to merge the two snippets - anyone know how to go about this?

+2  A: 

Try preventing the default action of the enter key before calling the submit.

Ganesh Shankar
+2  A: 

Usually this means that both your link and the browser's default mechanism are submitting the form. To prevent that, put a handler on the form's submit event that prevents (stops) it (the event is not fired when you submit the form programmatically, so it won't prevent your link sending it).

T.J. Crowder
+2  A: 

A form will submit automatically when you click enter, there is no need to write the code yourself. If you want to add some testing to the enterkey event before you submit, then you can return false from the callback function to prevent the default browser action.

Marius