There seem to be several ways to submit (POST) disabled form fields in jQuery:
I was wondering which (if any) is considered best practice for submitting disabled form fields. Obviously readOnly
is the best option when it's available, but I have checkboxes that I need to submit even though they are disabled (due to business logic). I realize this is not an ideal situation, but rarely is that the case in web development.
Is there a best-practice for submitting disabled form elements?
Tip: Disabled <input> elements in a form will not be submitted!
You can disable form fields by using some CSS. To disable form fields, use the CSS pointer-events property set to “none”.
You can use $(":disabled") to select all disabled items in the current context. To determine whether a single item is disabled you can use $("#textbox1").is(":disabled") . Save this answer.
Use $("#div1"). prop("disabled") to check whether the div is disabled or not.
The best option is to make the inputs readonly - create a click event for the check boxes that simply returns false, and change their background color.
There is no best practice, but that one requires the least fudging.
A fourth solution would be to enable the check boxes before submitting the form:
$("form").submit(function() {
$("input:checkbox", this).prop("disabled", false);
});
You can use a more sophisticated selector if you do not want to re-enable all the check boxes.
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