When i used any of the following code ,select
element do looks like disabled,but the select is not pass on the server : Im thinking of the readonly
to be used, but i dont know or is that will solved the issue. Any help is much appreciated.
$('#selectID').prop('disabled',true);
$('#selectID').prop('disabled','disabled');
$('#selectID').attr('disabled',true);
$('#selectID').attr('disabled','disabled');
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 disabled attribute is a boolean attribute. When present, it specifies that an option should be disabled. A disabled option is unusable and un-clickable. The disabled attribute can be set to keep a user from selecting the option until some other condition has been met (like selecting a checkbox, etc.).
Instead of disabling the select , disable all of the option s and use CSS to gray out the select so it looks like its disabled. Add a click event handler to the submit button so that it enables all of the disabled dropdown menus before submitting the form.
We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element. A disabled drop-down list is un-clickable and unusable.
see this answer - 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 it's value to the hidden input in an onchange event.
see this fiddle to demnstrate how to extract the selected value in a disabled select into a hidden field that will be submitted in the form.
$(function() {
var select_val = $('#sel_test option:selected').val();
$('#hdn_test').val(select_val);
$('#output').text('Selected value is: ' + select_val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select disabled="disabled" id="sel_test">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="hidden" id="hdn_test" />
<div id="output"></div>
hope that helps.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With