Reputation: 34011
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?
Upvotes: 7
Views: 6786
Reputation: 95
There is another way if require to post all or certain disbaled inputs:
Step2: onSubmit, enabled all the those fields following the example below:
$("input[class=mod]").removeAttr('disabled');
Step3: Post data
Step4: Disable those fields again (if required) following the example below:
$("input[class=mod]").attr('disabled="disabled"');
Upvotes: 0
Reputation: 263107
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.
Upvotes: 5
Reputation: 1738
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.
Upvotes: 8