search
HomeWeb Front-endHTML TutorialDetailed introduction to the usage of single-select and multiple-select tags in HTML

The

select element creates a single-select or multiple-select menu. When submitting a form, the browser will submit the selected items, or collect multiple options separated by commas. The following is an example of its specific usage. The select element creates single-select or multiple-select menus. When the form is submitted, the browser submits the selected items, or collects multiple comma-separated options, combines them into a single parameter list, and includes the name attribute when submitting the

The code is as follows:

<select> 
<option value ="volvo">Volvo</option> 
<option value ="saab">Saab</option> 
<option value="opel">Opel</option> 
<option value="audi">Audi</option> 
</select>


Among them, the tag can be omitted and used in the page

The code is as follows:

<SELECT NAME="studyCenter" id="studyCenter" SIZE="1"> 
<OPTION VALUE="0">全部 
<OPTION VALUE="1">湖北电大网络学习中心 
<OPTION VALUE="2">成都师范学院网络学习中心 
<OPTION VALUE="3">武汉职业技术学院网络学习中心 
</SELECT>


2. The Select element can also be used for multiple selections, see the following code:

The code is as follows:

//有multiple属性,则可以多选 
<select name= “education” id=”education” multiple=”multiple”> 
<option value=”1”>高中</option> 
<option value=”2”>大学</option> 
<option value=”3”>博士</option> 
</select> 
//下面没有multiple属性 , 只显示一条,不能多选 
<select name= “education” id=”education” > 
<option value=”1”>高中</option> 
<option value=”2”>大学</option> 
<option value=”3”>博士</option> 
</select> 
//下面是设置了size属性的情况 , 如果size = 3 那么就显示三条数据,注意不能多选的。 
<select name="education" id="education" size=&#39;3&#39;> 
<option value="0">小学</option> 
<option value="1">初中</option> 
<option value="2">高中</option> 
<option value="3">中专</option> 
<option value="4">大专</option> 
<option value="5">本科</option> 
<option value="6">研究生</option> 
<option value="7">博士</option> 
<option value="8">博士后</option> 
<option selected>请选择</option> 
</select>


3. All common operations involved in the multi-select Select component:

1. Determine whether there is an Item with a specified value in the select option

The code is as follows:

@param objSelectId 将要验证的目标select组件的id 
@param objItemValue 将要验证是否存在的值 
function isSelectItemExit(objSelectId,objItemValue) { 
var objSelect = document.getElementById(objSelectId); 
var isExit = false; 
if (null != objSelect && typeof(objSelect) != "undefined") { 
for(var i=0;i<objSelect.options.length;i++) { 
if(objSelect.options[i].value == objItemValue) { 
isExit = true; 
break; 
} 
} 
} 
return isExit; 
}


2. Add an Item to the select option

The code is as follows:

@param objSelectId 将要加入item的目标select组件的id 
@param objItemText 将要加入的item显示的内容 
@param objItemValue 将要加入的item的值 
function addOneItemToSelect(objSelectId,objItemText,objItemValue) { 
var objSelect = document.getElementById(objSelectId); 
if (null != objSelect && typeof(objSelect) != "undefined") { 
//判断是否该值的item已经在select中存在 
if(isSelectItemExit(objSelectId,objItemValue)) { 
$.messager.alert(&#39;提示消息&#39;,&#39;该值的选项已经存在!&#39;,&#39;info&#39;); 
} else { 
var varItem = new Option(objItemText,objItemValue); 
objSelect.options.add(varItem); 
} 
} 
}


3. Delete the selected item from the select option, support multiple selections and multiple deletions

The code is as follows:

@param objSelectId 将要进行删除的目标select组件id 
function removeSelectItemsFromSelect(objSelectId) { 
var objSelect = document.getElementById(objSelectId); 
var delNum = 0; 
if (null != objSelect && typeof(objSelect) != "undefined") { 
for(var i=0;i<objSelect.options.length;i=i+1) { 
if(objSelect.options[i].selected) { 
objSelect.options.remove(i); 
delNum = delNum + 1; 
i = i - 1; 
} 
} 
if (delNum <= 0 ) { 
$.messager.alert(&#39;提示消息&#39;,&#39;请选择你要删除的选项!&#39;,&#39;info&#39;); 
} else { 
$.messager.alert(&#39;提示消息&#39;,&#39;成功删除了&#39;+delNum+&#39;个选项!&#39;,&#39;info&#39;); 
} 
} 
}


4. Delete an Item from the select option according to the specified value

The code is as follows:

@param objSelectId 将要验证的目标select组件的id 
@param objItemValue 将要验证是否存在的值 
function removeItemFromSelectByItemValue(objSelectId,objItemValue) { 
var objSelect = document.getElementById(objSelectId); 
if (null != objSelect && typeof(objSelect) != "undefined") { 
//判断是否存在 
if(isSelectItemExit(objSelect,objItemValue)) { 
for(var i=0;i<objSelect.options.length;i++) { 
if(objSelect.options[i].value == objItemValue) { 
objSelect.options.remove(i); 
break; 
} 
} 
$.messager.alert(&#39;提示消息&#39;,&#39;成功删除!&#39;,&#39;info&#39;); 
} else { 
$.messager.alert(&#39;提示消息&#39;,&#39;不存在指定值的选项!&#39;,&#39;info&#39;); 
} 
} 
}


5. Clear all options in the select

The code is as follows:

@param objSelectId 将要进行清空的目标select组件id 
function clearSelect(objSelectId) { 
var objSelect = document.getElementById(objSelectId); 
if (null != objSelect && typeof(objSelect) != "undefined") { 
for(var i=0;i<objSelect.options.length;) { 
objSelect.options.remove(i); 
} 
} 
}


6. Get all the items in the select and assemble all values ​​into one character String, separated by commas between values ​​

The code is as follows:

@param objSelectId 目标select组件id 
@return select中所有item的值,值与值之间用逗号隔开 
function getAllItemValuesByString(objSelectId) { 
var selectItemsValuesStr = ""; 
var objSelect = document.getElementById(objSelectId); 
if (null != objSelect && typeof(objSelect) != "undefined") { 
var length = objSelect.options.length 
for(var i = 0; i < length; i = i + 1) { 
if (0 == i) { 
selectItemsValuesStr = objSelect.options[i].value; 
} else { 
selectItemsValuesStr = selectItemsValuesStr + "," + objSelect.options[i].value; 
} 
} 
} 
return selectItemsValuesStr; 
}


7. Move all selected options in one select to another select Go

The code is as follows:

@param fromObjSelectId 移动item的原select组件id 
@param toObjectSelectId 移动item将要进入的目标select组件id 
function moveAllSelectedToAnotherSelectObject(fromObjSelectId, toObjectSelectId) { 
var objSelect = document.getElementById(fromObjSelectId); 
var delNum = 0; 
if (null != objSelect && typeof(objSelect) != "undefined") { 
for(var i=0;i<objSelect.options.length;i=i+1) { 
if(objSelect.options[i].selected) { 
addOneItemToSelect(toObjectSelectId,objSelect.options[i].text,objSelect.options[i].value) 
objSelect.options.remove(i); 
i = i - 1; 
} 
} 
} 
}


8. Move all options in one select to another select

The code is as follows :

@param fromObjSelectId 移动item的原select组件id 
@param toObjectSelectId 移动item将要进入的目标select组件id 
function moveAllToAnotherSelectObject(fromObjSelectId, toObjectSelectId) { 
var objSelect = document.getElementById(fromObjSelectId); 
if (null != objSelect) { 
for(var i=0;i<objSelect.options.length;i=i+1) { 
addOneItemToSelect(toObjectSelectId,objSelect.options[i].text,objSelect.options[i].value) 
objSelect.options.remove(i); 
i = i - 1; 
} 
} 
}

For more detailed introduction to the single-select and multi-select usage of the select tag in HTML, please pay attention to the PHP Chinese website for related articles!

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
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version