HTML button to NOT submit form
I think this is the most annoying little peculiarity of HTML... That button needs to be of type "button" in order to not submit.
<button type="button">My Button</button>
Update 5-Feb-2019: As per the HTML Living Standard (and also HTML 5 specification):
The missing value default and invalid value default are the Submit
Button state.
Can I make a button not submit a form?
The default value for the type
attribute of button
elements is "submit". Set it to type="button"
to produce a button that doesn't submit the form.
<button type="button">Submit</button>
In the words of the HTML Standard: "Does nothing."
Disable form auto submit on button click
Buttons like <button>Click to do something</button>
are submit buttons.
Set type="button"
to change that. type="submit"
is the default (as specified by the HTML spec):
The missing value default and invalid value default are the Submit Button state.
Keep button from submitting form Javascript
Since you're not using a type="submit"
the only reason your form would submit anything is because you're literally calling the submit() method when the button is clicked. (well, sort of. It's actually form.submit()
- the method you created is window.submit()
).
By default, an input of type="button"
will not do a form submission unless you literally call form.submit()
Prevent users from submitting a form by hitting Enter
You can use a method such as
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:
function validationFunction() {
$('input').each(function() {
...
}
if(good) {
return true;
}
return false;
}
$(document).ready(function() {
$(window).keydown(function(event){
if( (event.keyCode == 13) && (validationFunction() == false) ) {
event.preventDefault();
return false;
}
});
});
button inside form should not submit form
Add type="button"
to your signup button
Disable submit button on form submit
Do it onSubmit()
:
$('form#id').submit(function(){
$(this).find(':input[type=submit]').prop('disabled', true);
});
What is happening is you're disabling the button altogether before it actually triggers the submit event.
You should probably also think about naming your elements with IDs or CLASSes, so you don't select all inputs of submit type on the page.
Demonstration: http://jsfiddle.net/userdude/2hgnZ/
(Note, I use preventDefault()
and return false
so the form doesn't actual submit in the example; leave this off in your use.)
Related Topics
Set a Callback Function to a New Window in JavaScript
Adding Images to an HTML Document with JavaScript
Jquery: How to Listen for Dom Changes
How to Format Vuetify Data Table Date Column
How to Save Canvas Animation as Gif or Webm
HTML Mobile -Forcing the Soft Keyboard to Hide
How to Listen/Detect Changes to an Input Value - When the Input Value Is Changed via JavaScript
How to Reach the Element Itself Inside Jquery's 'Val'
Addeventlistener with Ajax Not Working Well
How to Prevent an Http Request Just for a Favicon
Ios8 Safari After a Pushstate the :Nth-Child() Selectors Not Works
How to Access Styles from React
Jquery Jump or Scroll to Certain Position, Div or Target on the Page from Button Onclick
Why Are CSS Animations and Transitions Blocked by JavaScript