How to show disable HTML select option in by defau

2019-01-13 03:35发布

I am new to HTML and PHP and want to achieve a drop-down menu from the mysql table and hard-coded too. I have multiple select in my page, One of them is

<select name="tagging">
    <option value="">Choose Tagging</option>
    <option value="Option A">Option A</option>
    <option value="Option B">Option B</option>
    <option value="Option C">Option C</option>
</select>

Problem is now that user can also select "Choose Tagging" as his tagging but i only want to provide him to chose from available three. I used disable as

<select name="tagging">
    <option value="" disabled="disabled">Choose Tagging</option>
    <option value="Option A">Option A</option>
    <option value="Option B">Option B</option>
    <option value="Option C">Option C</option>
</select>

But now "Option A" became the default one. So i want to set "Choose Tagging" as by default and also want to disable it from selection. Is it a way to do this. Same thing need to be done with other select which will fetch data from Mysql. Any suggestion will be appreciable.

11条回答
ら.Afraid
2楼-- · 2019-01-13 04:33

In HTML5, to select a disabled option:

<option selected disabled>Choose Tagging</option>
查看更多
时光不老,我们不散
3楼-- · 2019-01-13 04:33
 selected disabled="true"

Use this. It will work in new browsers

查看更多
Deceive 欺骗
4楼-- · 2019-01-13 04:35

Use hidden.

<select>
  <option hidden>Choose</option>
  <option>Item 1</option>
  <option>Item 2</option>
</select>

This doesn't unset it but you can however hide it in the options while it's displayed by default.

jsfiddle

查看更多
We Are One
5楼-- · 2019-01-13 04:36

Another SELECT tag solution for those who want to keep first option blank.

<label>Unreal :</label>
<select name="unreal">
   <option style="display:none"></option>
   <option>Money</option>
   <option>Country</option>
   <option>God</option>
</select>

查看更多
SAY GOODBYE
6楼-- · 2019-01-13 04:39

I know you ask how to disable the option, but I figure the end users visual outcome is the same with this solution, although it is probably marginally less resource demanding.

Use the optgroup tag, like so :

<select name="tagging">
    <optgroup label="Choose Tagging">
        <option value="Option A">Option A</option>
        <option value="Option B">Option B</option>
        <option value="Option C">Option C</option>
    </optgroup>
</select>
查看更多
登录 后发表回答