The form is submitted twice when you press return / enter

I use anchors as links, and I also bind an input / return key to submit the form, for example:

$("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 is submitted twice when you press Enter / Return using the code above, I need to combine the two fragments - does anyone know how to do this?

+4
source share
3 answers

The form will be automatically submitted when you press the "Enter" button, there is no need to write the code yourself. If you want to add any testing to the enterkey event before sending, you can return false from the callback function to prevent the browser from acting by default.

+5
source

Try to prevent the default action of the input key before calling submit.

+2
source

This usually means that both your link and the default browser mechanism submit the form. To prevent this from happening, put a handler on an event of the submit form that prevents (stops) it (the event does not fire when you submit the form programmatically, so this will not interfere with sending your link).

+2
source

Source: https://habr.com/ru/post/1301789/


All Articles