HTML form readonly SELECT tag/input
You should keep the select
element disabled
but also add another hidden input
with the same name and value.
If you reenable your SELECT, you should copy its value to the hidden input in an onchange event and disable (or remove) the hidden input.
Here is a demo:
$('#mainform').submit(function() { $('#formdata_container').show(); $('#formdata').html($(this).serialize()); return false;});
$('#enableselect').click(function() { $('#mainform input[name=animal]') .attr("disabled", true); $('#animal-select') .attr('disabled', false) .attr('name', 'animal'); $('#enableselect').hide(); return false;});
#formdata_container { padding: 10px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div> <form id="mainform"> <select id="animal-select" disabled="true"> <option value="cat" selected>Cat</option> <option value="dog">Dog</option> <option value="hamster">Hamster</option> </select> <input type="hidden" name="animal" value="cat"/> <button id="enableselect">Enable</button> <select name="color"> <option value="blue" selected>Blue</option> <option value="green">Green</option> <option value="red">Red</option> </select>
<input type="submit"/> </form></div>
<div id="formdata_container" style="display:none"> <div>Submitted data:</div> <div id="formdata"> </div></div>
how to set select element as readonly ('disabled' doesnt pass select value on server)
To be able to pass the select
, I just set it back to :
$('#selectID').prop('disabled',false);
or
$('#selectID').attr('disabled',false);
when passing the request.
HTML form readonly SELECT tag/input
You should keep the select
element disabled
but also add another hidden input
with the same name and value.
If you reenable your SELECT, you should copy its value to the hidden input in an onchange event and disable (or remove) the hidden input.
Here is a demo:
$('#mainform').submit(function() { $('#formdata_container').show(); $('#formdata').html($(this).serialize()); return false;});
$('#enableselect').click(function() { $('#mainform input[name=animal]') .attr("disabled", true); $('#animal-select') .attr('disabled', false) .attr('name', 'animal'); $('#enableselect').hide(); return false;});
#formdata_container { padding: 10px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div> <form id="mainform"> <select id="animal-select" disabled="true"> <option value="cat" selected>Cat</option> <option value="dog">Dog</option> <option value="hamster">Hamster</option> </select> <input type="hidden" name="animal" value="cat"/> <button id="enableselect">Enable</button> <select name="color"> <option value="blue" selected>Blue</option> <option value="green">Green</option> <option value="red">Red</option> </select>
<input type="submit"/> </form></div>
<div id="formdata_container" style="display:none"> <div>Submitted data:</div> <div id="formdata"> </div></div>
How to make option element readonly but not disabled the select element
You can use disabled
on options
instead of the whole select
tag
<select name="type" tabindex="0"> <option value="" disabled></option> <option value="first" disabled>first</option> <option value="second" disabled>second</option></select>
Make select dropdown readonly and required
In Html5 doesnot support required
and readonly
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 .
<body>
<form action="/action_page.php" ><select class="form-control" id="UnidadMedida" required> <option value="" selected="selected"></option> <option value="Unidad">Unidad</option> <option value="Kg">Kilogramo</option> <option value="oz">Onzas</option> <option value="l">Litro</option> <option value="gal">Galon</option> <option value="m">Metro</option> <option value="min">Minuto</option> <option value="h">Hora</option> <option value="d">Dia</option> <option value="ml">Mililitro</option> <option value="g">Gramo</option> <option value="t">Tonelada</option> <option value="sp">Servicios Profesionales</option> </select><input type="submit"></form>
</body>
:read-only selector not working on select tag?
You need to assign a unique identifier such as a class
or an id
to specifically make the first select tag readonly
and not both.. for example, here, am using tag and an attribute selector to select the tags which have readonly
attribute
select[readonly]:-moz-read-only { /* For Firefox */ pointer-events: none;}
select[readonly]:read-only { pointer-events: none;}
<p> ACCEPTED </p><select readonly> <option>1</option> <option>2</option> <option>3</option></select><br><p> UNACCEPTED : selector also applying here </p><select> <option>1</option> <option>2</option> <option>3</option></select>
Related Topics
How to Get the Element Clicked (For the Whole Document)
Animate Element to Auto Height With Jquery
When Is Nodelist Live and When Is It Static
How to Prevent Iframe from Redirecting Top-Level Window
How to Hide the Html5 Number Input'S Spin Box
Onclick to Get the Id of the Clicked Button
Submit Two Forms With One Button
How to Get an HTML Element'S Style Values in JavaScript
Load and Execution Sequence of a Web Page
Html/JavaScript: How to Access Json Data Loaded in a Script Tag With Src Set
Retrieving the Text of the Selected ≪Option≫ in ≪Select≫ Element
How to Make a Link Open Multiple Pages When Clicked
How to Set the Form Action Through JavaScript
Force a Browser to Save File as After Clicking Link
How to Scroll an HTML Page to a Given Anchor