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?
Rather than the select itself, you could disable all of the options except for the currently selected option. This gives the appearance of a working drop-down, but only the option you want passed in is a valid selection.
Set the select disabled when you plan for it to be read-only and then remove the disabled attribute just before submitting the form.
Simple jQuery solution
Use this if your selects have the
readonly
classOr this if your selects have the
readonly="readonly"
attributeSimply, remove the disabled attribute before submit the form.
disabled="disabled" ->
will get your value from database dan show it in the form.readonly="readonly" ->
you can change your value in selectbox, but your value couldn't save in your database.If the select dropdown is read-only since birth and does not need to change at all, perhaps you should use another control instead? Like a simple
<div>
(plus hidden form field) or an<input type="text">
?Added: If the dropdown is not read-only all the time and JavaScript is used to enable/disable it, then this is still a solution - just modify the DOM on-the-fly.