Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best method for submitting disabled form fields in jQuery?

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

  • Have a hidden field that changes when the input changes, and submit that
  • Manually append the key/value pairs upon submission
  • Revert values on the server-side (only if values not expected to change)

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?

like image 499
Igor Avatar asked Jan 13 '12 18:01

Igor


People also ask

Are disabled form fields submitted?

Tip: Disabled <input> elements in a form will not be submitted!

How do I disable a form field?

You can disable form fields by using some CSS. To disable form fields, use the CSS pointer-events property set to “none”.

How check textbox is disabled or not in jquery?

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.

How can check Div is disabled or not in jquery?

Use $("#div1"). prop("disabled") to check whether the div is disabled or not.


2 Answers

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.

like image 94
Jonathan Rich Avatar answered Oct 22 '22 21:10

Jonathan Rich


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.

like image 34
Frédéric Hamidi Avatar answered Oct 22 '22 20:10

Frédéric Hamidi