I have a simple html form:
<form action="test" method="post" id="myForm">
<input type="text" name="myTextField">
<input type="text" name="myTextField2">
<input type="text" name="dontSubmitThisField">
</form>
And I need to submit it with JavaScript, but I want to exclude the dontSubmitThisField
field from the request. Is there a way of doing that without Ajax?
A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.
You can use preventDefault method of the event object. Show activity on this post. Alternatively, you could use event.
The disabled Attribute The value of a disabled input field will not be sent when submitting the form!
Use the return value of the function to stop the execution of a form in JavaScript.
Just add form="nosubmit" to the input. This assigns the field to a different form altogether, so it won't be submitted.
Just disable the field.
Either do it via HTML if the field cannot be updated by the user: http://www.w3schools.com/tags/att_input_disabled.asp
Or do it via jQuery with a on submit event:
$('#myForm').submit(function(){
$('input[name="dontSubmitThisField"]').prop('disabled', true);
});
Disabled fields or fields without a name
attribute won't submit.
However, if somehow you want to name your fields and do not want to disable them upfront, you could intercept all form submissions and disable every fields that have a data-no-submit
attribute.
document.addEventListener('submit', function (e) {
if (!e.defaultPrevented) {
[].forEach.call(e.target.querySelectorAll('[data-no-submit]'), function (field) {
field.disabled = true;
});
}
});
Then you can just do:
<input type="text" name="dontSubmitThisField" data-no-submit>
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