Best way to submit disabled form fields in jQuery?

There seem to be several ways to submit (POST) disabled form fields in jQuery:

  • Enter a hidden field that changes when the input changes and send it
  • Manually add key / value pairs when sending
  • Return server-side values ​​(only if the values ​​do not change)

I was wondering which (if any) is considered best practice for submitting prohibited form fields. Obviously, readOnly is the best option when it is available, but I have checkboxes that I need to send, even if they are disabled (due to business logic). I understand that this is not an ideal situation, but this is rarely the case in web development.

Is there a best practice for submitting disabled form elements?

+7
source share
3 answers

The best option is to make the data read-only - create a click event for checkboxes that simply return false and change their background color.

There is no best practice, but it requires the least fudging.

+7
source

A fourth solution would be to enable the checkboxes 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 turn on all the checkboxes again.

+5
source

There is another way if you want to publish all or some of the discrete inputs:

  • Step1: provide the name of the class name (e.g. mod).
  • Step 2: onSubmit, included all of the fields below:

    $ ("input [class = mod]") removeAttr ('disabled') ;.

  • Step 3: Published Data

  • Step 4: Disable these fields again (if necessary) in the following example:

    $ ("input [class = mods]") atr ('disabled = "disabled"') ;.

0
source

All Articles