'Form submits twice when pressing return/enter
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?
Solution 1:[1]
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).
For example:
document.querySelector("selector-for-your-form").addEventHandler("submit", event => {
event.preventDefault(); // Prevents submission
});
Solution 2:[2]
Try preventing the default action of the enter key before calling the submit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ganesh Shankar |
