How to show disabled HTML select option by default? Ask Question

How to show disabled HTML select option by default? Ask Question

I am new to HTML and PHP and want to achieve a drop-down menu from a MySQL table and hard-coded options too. I have multiple select elements 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>

The problem is that now the user can also select "Choose Tagging" as an option, but I only want them to choose from the available three. I then used disabled as follows:

<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" becomes the default one. So I want to set "Choose Tagging" by default and also want to disable it from selection. The same thing needs to be done with another select which will fetch data from MySQL.

Is there a way to do this?

ベストアンサー1

Use

<option selected="true" disabled="disabled">Choose Tagging</option>

おすすめ記事