Igor
Igor

Reputation: 34011

Best method for submitting disabled form fields in jQuery?

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

Answers (3)

origin
origin

Reputation: 95

There is another way if require to post all or certain disbaled inputs:

  1. Step1: give a class name name (eg. mod).
  2. Step2: onSubmit, enabled all the those fields following the example below:

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

  3. Step3: Post data

  4. Step4: Disable those fields again (if required) following the example below:

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

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

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

Jonathan Rich
Jonathan Rich

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

Related Questions