Home  >  Article  >  Web Front-end  >  What are the uses of select tag in HTML?

What are the uses of select tag in HTML?

php中世界最好的语言
php中世界最好的语言Original
2018-02-03 09:28:503811browse

This time I will show you how to use the select tag in HTML, and what are the precautions for using the select tag in HTML. The following is a practical case, let's take a look. The

select element creates a single-select or multiple-select menu. When the form is submitted, the browser submits the selected items, or collects multiple options separated by commas, combines them into a single parameter list, and includes the name when submitting the 221f08282418e2996498697df914ce4e form data to the server

Attributes.
1. Basic usage:

<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 4afa15d3069109ac30911f04c56f3338 tag can be omitted, and the usage in the page


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

2. The Select element is OK For multiple selections, see the following code:


//有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>

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

@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

@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, supports multiple selections and multiple deletions

@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 according to the specified value from the select option


@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

@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 items in the select and assemble all values ​​into a

string
, separate values ​​with commas

@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; 
}
I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

How to fix the width of table table-layout: fixed


Setting the width of table cell td How to solve invalid problem

The above is the detailed content of What are the uses of select tag in HTML?. 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