The simplest solution to prevent the form submission is to return false on submit event handler defined using the onsubmit property in the HTML <form> element.
To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.
The disabled Attribute The value of a disabled input field will not be sent when submitting the form!
Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.
You could insert input fields without "name" attribute:
<input type="text" id="in-between" />
Or you could simply remove them once the form is submitted (in jQuery
):
$("form").submit(function() {
$(this).children('#in-between').remove();
});
The easiest thing to do would be to insert the elements with the disabled
attribute.
<input type="hidden" name="not_gonna_submit" disabled="disabled" value="invisible" />
This way you can still access them as children of the form.
Disabled fields have the downside that the user can't interact with them at all- so if you have a disabled
text field, the user can't select the text. If you have a disabled
checkbox, the user can't change its state.
You could also write some javascript to fire on form submission to remove the fields you don't want to submit.
Simple try to remove name attribute from input element.
So it has to look like
<input type="checkbox" checked="" id="class_box_2" value="2">
I just wanted to add an additional option: In your input add the form tag and specify the name of a form that doesn't exist on your page:
<input form="fakeForm" type="text" readonly value="random value" />
You can write an event handler for onsubmit
that removes the name
attribute from all of the input fields that you want not to be included in the form submission.
Here's a quick untested example:
var noSubmitElements = [ 'someFormElementID1', 'someFormElementID2' ]; //...
function submitForm() {
for( var i = 0, j = noSubmitElements.length; i < j; i++ ) {
document.getElementById(noSubmitElements[i]).removeAttribute('name');
}
}
form.onsubmit = submitForm;
I know this post is ancient, but I'll reply anyway. The easiest/best way I have found is just to simply set the name to blank.
Put this before your submit:
document.getElementById("TheInputsIdHere").name = "";
All in all your submit function might look like this:
document.getElementById("TheInputsIdHere").name = "";
document.getElementById("TheFormsIdHere").submit();
This will still submit the form with all of your other fields, but will not submit the one without a name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With