36
I believe most of you here know the difference between readonly
and disabled
.
Select Readonly
The readonly
does not apply properly to the select
<select name="field" readonly="readonly">
<option value=""></option>
<option value="1">Cliente</option>
<option value="2">Contador</option>
<option value="3">Vendedor</option>
</select>
As you can see I can still change the value of select
Select Disabled
What does not happen with the disabled
that actually blocks select
<select name="field" disabled="disabled">
<option value=""></option>
<option value="1" selected>Cliente</option>
<option value="2">Contador</option>
<option value="3">Vendedor</option>
</select>
Here I can not edit, but it is also sent by the form.
Goal
I need to send the content of select by form, but the user can not edit it, its content is auto selected by other factors.
Doubt
- How to block the
select
, but still send its value byform
? - It would be possible without using js?
Addendum
This way I have what I want, but I would like a more elegant method, here he can still see the options.
<select name="field">
<option value="" disabled="disabled"></option>
<option value="1" disabled="disabled" selected>Cliente</option>
<option value="2" disabled="disabled">Contador</option>
<option value="3" disabled="disabled">Vendedor</option>
</select>
Does it not send any value when the field is disabled? You send this data via Ajax?
– Laerte
@Laerte exactly he even sends the
name
from select, it’s as if it didn’t even exist. Here I use ajax, but I didn’t intend to take content by js, orjquery.param
– Guilherme Lautert
If you use jquery: you leave the select field enabled send the form c/ Selected value and only then use jquery to disable the field. using $( Document ).ready( ... )
– FernandoLopes
Wouldn’t it be simpler to show a normal input text in this case? I usually only put select when the person can change the data. If it is readonly from the server, the job of changing a "readonly" or input type is the same. If you are going to use JS, it is the case that you only change the input type dynamically too.
– Bacco
I agree with @Bacco, if the user cannot change or view the other options, what is the need to use a select? If you want to show that this field may be edited in the future I think disabled would be the best option. Basically at this point you are deceiving the user as it will look like a combo box that is not working properly.
– lazyFox
@lazyFox an even more elegant option than the most voted answer currently (which I disagree is a real solution) is by only one option with the desired value in the cases "readonly" kkkkkk
– Bacco
Exactly, no more XD
– lazyFox
@Bacco that I remember (a long time ago), I wanted to do this for a form, registration, which was used tbm to edit, however in the creation the select was released, but in Edit not.
– Guilherme Lautert
@Guilhermelautert I believe that a JS changing the type of input when entering the editing mode is still nicer than trying to "cover" the access to the field as proposed in some answers.
– Bacco
@Bacco I don’t disagree, but then we start to get into personal tastes, there goes each one. The good thing is to have options :D
– Guilherme Lautert
@Guilhermelautert what worries me is to function without side effects, nor do I think it is taste. But I think you already got the idea, which thing after we detailed better in the chat to not lengthen too much here ;) - Someone of the most trusted users of the site gave a cool idea too (by other means), we would have to test the validity: put disabled in all options of select (less than desired) to simulate readonly.
– Bacco
If Voce will not use select for editing, display the content in an input text.
– Marcelo Mota