This question already has answers here:
Prevent users from submitting a form by hitting Enter
(36 answers)
Closed 2 years ago.
Is there a way to make a form NOT refresh or call anything when you hit "Enter" key on your keyboard?
Thank you so much!!!
I found this code for preventing Enter from working, but it DOESN'T work in IE :(
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
}
Try this:
$(function() {
$("form").submit(function() { return false; });
});
Disabling the submit event isn't a good idea. Now you can never submit the form by pressing the button where it is for.
Rather hook on the keypress event:
<form onkeypress="return event.keyCode != 13">
or in jQuery flavor:
$('form').keypress(function(event) {
return event.keyCode != 13;
});
13 is the keyCode of Enter key. This works in all browsers from IE6 up to with all the current ones. You only have to take textareas into account. You may then consider this construct instead:
$(':input:not(textarea)').keypress(function(event) {
return event.keyCode != 13;
});
add onSubmit property on form's tag.
<form onSubmit="return false;">
You can prevent form submission by 'enter' key natively if you are using AngularJS.
According to HTML specification and AngularJS, you need to check this list:
Form must have NO action=... attribute (AngularJS handles it automatically)
Form must have NO button with type="submit" attribute
So, if you have no action attribute and submit button, then your form should not be submitted by hitting enter key.
Also, cross-browser keyCode is this:
var code = (e.keyCode ? e.keyCode : e.which);
Related
Have been searching for a solution to this problem for 2 days now and none of the suggested solutions have worked so far.
My form html is defined with
<form id="quote_form" action="" method="get" class="ui large form">
and input text fields in the form
<input v-model="city" type="text" name="quote[city]" id="city">
I have been trying to isolate the cause of the issue but have not been able to do so. I tried turning off the keyboard shortcuts settings for semantic ui forms:
$('.ui.form').form({
keyboardShortcuts: false
});
I have also tried to override the enter key and prevent it from triggering the submit function in these ways:
$('#quote_form').on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
e.stopPropagation();
e.preventDefault();
return false;
}
});
$(document).on("keypress", "form", function(event) {
return event.keyCode != 13;
});
$('#quote_form').bind('keypress keydown keyup', function(e){
if(e.keyCode == 13) { e.preventDefault(); }
});
The form has multiple steps in filling it in. Each step uses a button to permit the advance to the next step. When enter is pressed then it causes the form to redirect to the first step/tab of the form. The only case where it doesn't redirect is when the rules of the current step are not satisfied. The form submission is handled by a submit button where the button itself calls methods to validate and submit the form. I can't find any connection between enter submit behaviour and the button for submitting.
If I am missing any useful information to help isolate the cause then please let me know. I'm new to asking questions here and want to prevent my question from being considered bad as much as possible :)
Here is solution for you:
#submit.prevent
https://jsfiddle.net/4qpffycs/2/
Just use #keyup.enter.prevent at the end of the input markup. See VueJS Doc
By the way, you should try to use native VueJS Events instead of all redoing it with JQuery
The solution I found regards only Semantic UI without VueJS, but should be applicable here and gives a bit of an insight into the issue.
The problem arises from the fact that $("#formId").form({ ... }) registers an event handler for a button press and submitting the form on Enter press if one of the input boxes are selected. This can be seen in Chrome DevTools when selecting the element and choosing Event Listeners category:
The simplest way I found to remove this behavior is to call
$("#formId").unbind('keydown')
to remove the keydown bind completely from the element.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Prevent Users from submitting form by hitting enter
I wrote some simple code (you can also test it here )
<form >
<input id="id2" type='text' />
</form>
$("#id2").keydown(function(e) {
if (e.keyCode == 13) {
alert('nope!');
return false;
}
});
If I click enter I should see an alert with "nope!" and then nothing. But, after I click "ok" on the alert window, the form is submitting despite the function returning false.
What is it? Something strange multithreading in javascript? Bug? Or...
Use the keypress event rather than the keydown event:
$("#id2").keypress(function(e) {
if (e.keyCode == 13) {
alert('nope!');
return false;
}
});
Updated demo
I'm solving the problem with default submit button in IE7. When I press "enter" key in input field, than some button on the page is clicked. So I've found the solution for this:
$(document).bind("keypress", function(ev) {
ev.keyCode == 13 && ev.preventDefault();
});
But there is the problem with this code: textarea tag don't get "new line". So I tried this:
$(document).bind("keypress", function(ev) {
if (ev.keyCode == 13 && ev.target.type != "textarea")
ev.preventDefault();
});
It works but looks dirty. The question is: can you advice better solution for the Problem?
Thank you in advance.
If it is to simply prevent the form submission on enter keypress, test on the keypress when the form is trying to submit...
$(your_form).submit(function(ev){
if (ev.keyCode == 13){
// Prevent form submission behaviors if the event was fired by enter keypress
ev.preventDefault();return false;
}
// And code for form submission here, or just keep the return true to make it behave normally.
return true;
});
This is not dirty. But if you want one line, you still could do with:
ev.keyCode == 13 && ev.target.type != "textarea" && ev.preventDefault();
A more elegant solution would be to use jQuery's submit handler and return false:
$('#myForm').submit(function(ev){
// custom form handling code here
return false;// prevent browser default form submission
});
Alternatively, you could also call .preventDefault() on the event object, passed in to the submit handler.
I had a jQuery / HTML code similar to this:
<form action="/SomeAction" method="post">
<input id="my-textbox" type="text" placeholder="Write something and press enter to continue..." />
</form>
<script type="text/javascript">
$(function() {
$('#my-textbox').keyup(function(e) {
var $textbox = $(this);
if ($textbox.val().length > 0 && e.keyCode == 13) {
$textbox.parent('form').submit();
}
});
});
</script>
The purpose was to automatically submit the form when the user pressed the Enter key. I regularly use Firefox so everything was OK for me until I tested in Google Chrome and Internet Explorer.
When I pressed the Enter key in the later browsers, sometimes I would get the form submitted twice. This was easy to notice because I would get duplicate entries in my DB and I'd see two POSTs using Fiddler.
After some testing, I found out that my problem was the jQuery code, since the textbox would submit automatically on enter without it, and using this code would produce a second POST in some browsers.
My questions are:
Why don't browsers smartly prevent the second form post (like Firefox did in my testing)?
Should I expect this behavior in all major browsers in all platforms?
Is there a way to improve this code so I perform the submit using JavaScript, but don't get the form submitted twice?
Why don't browsers smartly prevent the second form post (like Firefox did in my testing)?
That is the default behavior. What if you didn't have your script and the default behavior was such that the form wouldn't POST on enter.
Should I expect this behavior in all major browsers in all platforms?
Yes
Is there a way to improve this code so I perform the submit using JavaScript, but don't get the form submitted twice?
Use a global mutex variable and set it once the form POSTs - checking it on subsequent POSTs to validate. Or, return false from the keyup handler and stop the event propagation.
Some browsers will interpret an input button as a submit if there is only one button in the form. Just return false in your function to prevent the default behavior from submitting the form.
if ($textbox.val().length > 0 && e.keyCode == 13) {
$textbox.parent('form').submit();
return false;
}
Your form is being submitted right after the enter has been pressed (on keydown and before keyup fires) so you can do
$(function() {
$('#my-textbox').keydown(function(e){
if(e.keyCode==13) e.preventDefault();
});
$('#my-textbox').keyup(function(e) {
e.preventDefault();
var $textbox = $(this);
if($textbox.val().length > 0 && e.keyCode == 13) {
$textbox.parent('form').submit();
}
});
});
A simple test.
Add boolean variable that would be set to true after first submit and use that variable in your if condition. This would prevent accidental double click.
You should also prevent double submit in the application backend (many web frameworks have built-in mechanism for doing this, it easy to come up with custom solution as well).
This question already has answers here:
How can I execute a function on pressing the enter key in an <input> field?
(12 answers)
Closed 4 years ago.
im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to appear in my textarea (which is readonly)...
well look.. make long story short, i just want a basic enter key event handler, i know it works perfectly with submit buttons cus you don't need to program anything at all, its default. but my button is type="button" .... so when you press enter nothing happens... how do i trigger my button by pressing enter?
You could make the button type submit, or you can use the onkeyup event handler and check for keycode 13.
Here's a list of key codes: Javascript Char codes/Key codes). You'll have to know how to get the keycode from the event.
edit: an example
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
e.which = e.which || e.keyCode;
if(e.which == 13) {
// submit
}
}
Here is a working code snippet for listening for the enter key
$(document).ready(function(){
$(document).bind('keypress',pressed);
});
function pressed(e)
{
if(e.keyCode === 13)
{
alert('enter pressed');
//put button.click() here
}
}
Here is a version of the currently accepted answer (from #entonio) with key instead of keyCode:
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
if (e.key === 'Enter') {
// submit
}
}