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?
Tested and working in IE 6, 7 & 8b2, Firefox 2 & 3, Opera 9.62, Safari 3.2.1 for Windows and Google Chrome.
This is the simplest and best solution. You will set a readolny attr on your select, or anyother attr like data-readonly, and do the following
Easier still: add the style attribute to your select tag:
In addition to disabling the options that should not be selectable i wanted to actually make them dissapear from the list, but still be able to enable them should i need to later:
This finds all select elements with a readonly attribute, then finds all options inside those selects that are not selected, then it hides them and disables them.
It is important to separate the jquery query in 2 for performance reasons, because jquery reads them from right to left, the code:
will first find all unselected options in the document and then filter those that are inside selects with a readonly attribute.
Yet another more contemporary option (no pun intended) is to disable all the options of the select element other then the selected one.
note however that this is an HTML 4.0 feature and ie 6,7,8 beta 1 seem to not respect this.
http://www.gtalbot.org/BrowserBugsSection/MSIE7Bugs/OptionDisabledSupport.html
I managed it by hiding the select box and showing a
span
in its place with only informational value. On the event of disabling the.readonly
class, we need also to remove the.toVanish
elements and show the.toShow
ones.