Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make html <select> element look like "disabled", but pass values?

When I'm disabling a

<select name="sel" disabled>     <option>123</option> </select> 

element, it doesnt pass its variable.

What to do to look it like disabled, but be in "normal" state?

This is because I have a list of "selects", and sometimes some of them have single value, so user should understand that it has only one value without clicking it.

like image 346
el Dude Avatar asked Oct 07 '12 14:10

el Dude


People also ask

How do I make the select box disabled in HTML?

The disabled attribute for <select> element in HTML is used to specify that the select element is disabled. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.

How do you make a selection not editable in HTML?

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 disable a specific item in a dropdown element?

The drop-down is used to create a list of items that need to select an element. 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.

How do you disable a value in HTML?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!


2 Answers

You can keep it disabled as desired, and then remove the disabled attribute before the form is submitted.

$('#myForm').submit(function() {     $('select').removeAttr('disabled'); }); 

Note that if you rely on this method, you'll want to disable it programmatically as well, because if JS is disabled or not supported, you'll be stuck with the disabled select.

$(document).ready(function() {     $('select').attr('disabled', 'disabled'); }); 
like image 133
tomaroo Avatar answered Oct 02 '22 14:10

tomaroo


<select id="test" name="sel">   <option disabled>1</option>   <option disabled>2</option> </select>    

or you can use jQuery

$("#test option:not(:selected)").prop("disabled", true); 
like image 45
dingyuchi Avatar answered Oct 02 '22 15:10

dingyuchi