Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set select element as readonly ('disabled' doesnt pass select value on server)

Tags:

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');
like image 640
bumbumpaw Avatar asked Aug 14 '14 06:08

bumbumpaw


People also ask

How do you make a selection element readonly?

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 .

How do I make select option 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.).

How do you ensure a select form field is submitted when it is disabled?

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.

How do you make a drop down disabled in HTML?

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.


2 Answers

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.

like image 172
geevee Avatar answered Oct 22 '22 02:10

geevee


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.

like image 32
bumbumpaw Avatar answered Oct 22 '22 01:10

bumbumpaw