1

I tried setting readonly attribute to true, but it did not work as i am still able to select the value from dropdown.

<tr>
    <td>
        <select name="type" tabindex="0">
            <option value=""></option>
            <option value="first">first</option> 
            <option value="second">second</option>
        </select> 
    </td> 
</tr>
3
  • Actual code: <tr> <td><select name="type" tabindex="0"> <option value=""></option> <option value="first">first</option> <option value="second">second</option></select> </td> </tr> Commented Jan 27, 2020 at 5:47
  • 3
    where did you put readonly in your code? Commented Jan 27, 2020 at 5:51
  • 1
    Does this answer your question? How to disable an input type=text? Commented Jan 27, 2020 at 5:51

2 Answers 2

7

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> 

Sign up to request clarification or add additional context in comments.

8 Comments

Ok this way , Will I able to submit the value in the form ?
Yes you can submit the value in the form for the options which are "not disabled". The disabled ones will not be selected hence cannot be submitted.
No, basically I dont want user to select the value from dropdown but I need to myself set the value of dropdown according to some other fields below dropdown. So even I can hide also th options but whatevr value i set ..it should submit in form.
Ya you can give a default value to the select on the time of submission via js
Is it possible, If i stop user from expanding the dropdown itself?
|
2

Try this.

$(document).ready( function() {
  $('#select').change( function() {
    if($(this).val() !== '')  console.log($(this).val());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="type" tabindex="0" id="select"> 
     <option value=""></option> 
     <option value="first" disabled>first</option> 
     <option value="second" disabled>second</option>
      <option value="Not disabled">Not disabled</option>
</select> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.