Option 1 Option 1

Home >Web Front-end >Front-end Q&A >How to set up html drop-down box

How to set up html drop-down box

PHPz
PHPzOriginal
2023-04-27 16:38:237196browse

HTML (Hypertext Markup Language) is a commonly used web development language, and drop-down boxes often need to be set up on web pages. A drop-down box is a commonly used interactive control, usually used to allow users to select one from multiple options. Setting up a drop-down box in HTML is very simple. This article will introduce how to set up an HTML drop-down box.

How to set up the HTML drop-down box

The HTML drop-down box is created through the label is used to surround all options.

The following is a simple drop-down box example:

<select>
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

This code will generate a drop-down box containing three options.

Attributes of the drop-down box

By adding attributes, you can set various attributes of the drop-down box, such as selecting an option, setting the size of the drop-down box, etc.

The following are some commonly used attributes:

  1. selected attribute
<option selected value="选项1">选项1</option>

The selected attribute can be used to set the default option of the drop-down box. If selected is not set attribute, the default option is the first option.

  1. multiple attribute
<select multiple>
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

The multiple attribute enables the drop-down box to support multiple selections. Once multiple options are selected, you can hold down the Ctrl key and click on the options to deselect them.

  1. size attribute
<select size="5">
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

The size attribute can set the number of options that the drop-down box can display. If the number of options exceeds the specified number, a scroll bar appears to display additional options.

  1. disabled attribute
<select disabled>
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

The disabled attribute can make the drop-down box unavailable and prohibit the user from making selections.

  1. name attribute
<select name="mySelect">
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

The name attribute is used to specify the name of the drop-down box. This attribute is usually used for form submission or accessing the drop-down box in JavaScript.

  1. onchange attribute
<select onchange="myFunction()">
<option value="选项1">选项1</option>
<option value="选项2">选项2</option>
<option value="选项3">选项3</option>
</select>

The onchange attribute can be used to set the processing function after the drop-down box option is changed.

The above is the setting method of HTML drop-down box. I hope readers can master it.

The above is the detailed content of How to set up html drop-down box. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn