Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

HTML <select> tags


May 05, 2021 HTML Reference Manual


Table of contents


HTML and instructions for the definition and use of labels

The element is used to create a pull-down list.

The label in the element defines the options available in the list.


HTML <select> tags


Select elements create a single or multiple menu. /b10>When the form is submitted, the browser submits the selected item, or collects multiple options separated by commas, synthesizes it into a separate list of parameters, and includes the name property when submitting the form data to the server.

Tips and comments

Tip: The element is a form control that can be used to accept user input in a form.


The difference between HTML 4.01 and HTML5

HTML5 adds some new properties.

Create a selection list with 4 options:

<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

Try it out . . .

Browser support

HTML <select> tags HTML <select> tags HTML <select> tags HTML <select> tags HTML <select> tags

All major browsers support the label.


Property

New : The new property in HTML5.

Attributes value describe
autofocus New autofocus It is specified that the drop-down list is automatically obtained when the page is loaded.
disabled disabled When this property is TRUE, the drop-down list is disabled.
form New form_id Define one or more forms belonging to the Select field.
multiple multiple When this property is TRUE, multiple options can be selected.
name name Define the name of the drop-down list.
required New required Specifies that the user must select an option in a drop-down list before submitting the form.
size number Specify the number of visible options in the drop-down list.


Global properties

The label supports the global properties of HTML.


The event property

The tag supports the event properties of HTML.


Related articles

HTML DOM Reference Manual: Select Object

Summary of tips for using the label


1. Dynamic creation of select

function createSelect(){

var mySelect = document.createElement("select");

mySelect.id = "mySelect";

document.body.appendChild(mySelect);

}


2. Add option options

function addOption(){

Find objects based on id,

var obj=document.getElementById('mySelect');


Add an option

obj.add (new Option ("text," "value");

}


3. Remove all options options


function removeAll(){

var obj=document.getElementById('mySelect');


obj.options.length=0;


}


HTML <select> tags


4. Remove an option option


function removeOne(){

var obj=document.getElementById('mySelect');


index, to remove the serial number of the option, take the serial number of the currently selected option here


var index=obj.selectedIndex;

obj.options.remove(index);

}


5. Get the value of the option option


var obj=document.getElementById('mySelect');


var index=obj.selectedIndex; Ordinal number, taking the serial number of the currently selected option


var val = obj.options[index].value;


6. Get the text of the option option


var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; Ordinal number, taking the serial number of the currently selected option

var val = obj.options[index].text;


7. Modify the option option option option


var obj=document.getElementById('mySelect');

var index=obj.selectedIndex; Ordinal number, taking the serial number of the currently selected option

var val s obj.options s index s new Option ("new text", "new value");


HTML <select> tags

8. Delete the select


function removeSelect(){

var mySelect = document.getElementById("mySelect");

mySelect.parentNode.removeChild(mySelect);

}


9. Set select option is selected


function removeSelect(){

Dynamically add employees to the operator's pull-down list

for ( var i = 0; i < json.length; i++) {

var newOption = new Option(json[i].empname, json[i].empid, i);

Add employee information to the operator's pull-down list

objDeal.options.add(newOption);

The customer salesman's ID is not empty

if(empbyDealEmpId!="" || empbyDealEmpId!=0){

If the employee id is equal to the value in the drop-down list, the drop-down list is selected

if(empbyDealEmpId==objDeal.options[i].value){

Determine that this pull-down list is selected

objDeal.options[i].selected=true;

}

}

}

}


Case:

How do I make select in html unselectable?

Suppose you have a select, there are a few options in it, because the test needs to be fixed as one of the options, can not choose the other, how to do? I f disabled this select, the result is that there is no value at all. I s there any other way? ce.

Answer: Just put one option on it, or add a disabled to the option.
<form id="form1" name="form1" method="post" action="">
  <select name="select">
    <option>aa</option>
	<option disabled="disabled">bb</option>
	<option>cc</option>
  </select>
</form>


HTML <select> tags

How do I adjust the width of the select?


Answer: You can add a style style to the select label
<style>.s1{ width: 200px;}</style>
<select class="s1">
  <OPTION>很长很长也能显示</OPTION>
  <OPTION>很长很长也能显示</OPTION>
</select>

html select tag plus link 3 methods

html select tag plus link methods have many, next for you to introduce the next few more classic, interested friends can refer to, hope to help you.

The first:
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>select加链接</title> 
</head> 
<body> 
<SCRIPT language=javascript> 
<!-- 
// open the related site windows 
function mbar(sobj) { 
var docurl =sobj.options[sobj.selectedIndex].value; 
if (docurl != "") { 
open(docurl,'_blank'); 
sobj.selectedIndex=0; 
sobj.blur(); 
} 
} 
//--> 
</SCRIPT> 
<Select onchange=mbar(this) name="select"> 
<OPTION selected>=== 合作伙伴 ===</OPTION> 
<OPTION value="http://www.baidu.com">百度</OPTION> 
<OPTION value="http://www.w3cschool.cn">w3cschool在线教程</OPTION> 
<OPTION value="http://www.w3cschool.cn">优聚</OPTION> 
</Select> 
</body> 
</html> 

The second type:
<select name="pageselect" onchange="self.location.href=options[selectedIndex].value" > 
<OPTION value="http://www.baidu.com">百度</OPTION> 
<OPTION value="http://www.w3cschool.cn">w3cschool在线教程</OPTION> 
</select> 

The third type with a jump button:
<html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>select选择-按钮跳转</title> 
<script type="text/javascript"> 
function setsubmit() 
{ 
if(mylink.value == 0) 
window.location='http://www.baidu.com'; 
if(mylink.value == 1) 
window.location='http://www.w3cschool.cn'; 
if(mylink.value == 2) 
window.location='http://www.w3cschool.cn'; 
} 
</script> 
</head> 
<body> 
<select name="mylink" id="mylink"> 
<OPTION value="0">百度</OPTION> 
<OPTION value="1">w3cschool在线教程</OPTION> 
<OPTION value="2">优聚</OPTION> 
</select> 
<input type="button" id="btn" value="提交" onclick="setsubmit(this)" /> 
</body> 
</html> 

Expand your knowledge

SELECT can also be used as SQL ( Structured query language The query statement is used, SELECT statement Returns the result of the query in a database with the specified criteria, the result returned as a collection of records. For more information about SQL, you can refer to the full SQL Tutorial on this site!