According to HTML specs, the select
tag in HTML doesn't have a readonly
attribute, only a disabled
attribute. So if you want to keep the user from changing the dropdown, you have to use disabled
.
The only problem is that disabled HTML form inputs don't get included in the POST / GET data.
What's the best way to emulate the readonly
attribute for a select
tag, and still get the POST data?
Simple CSS solution:
This results in Select to be gray with nice "disable" cursor on hover
and on select the list of options is "empty" so you can not change its value.
We could also use this
Disable all except the selected option:
This way the dropdown still works (and submits its value) but the user can not select another value.
Demo
Here's an attempt to use a custom jQuery function to achieve the functionality (as mentioned here):
One simple server-side approach is to remove all the options except the one that you want to be selected. Thus, in Zend Framework 1.12, if $element is a Zend_Form_Element_Select:
If you are using jquery validate, you can do the following below, I used the disabled attribute without a problem:
You can re-enable the select object on submit.
EDIT: i.e., normally disabling the select tag (with the disabled attribute) and then re-enabling it automatically just before submiting the form:
Example with jQuery:
To disable it:
To re-enable it before submission so that GET / POST data is included:
In addition, you could re-enable every disabled input or select: