Detailed introduction to the usage of single-select and multiple-select tags in HTML

高洛峰
Release: 2017-03-06 15:23:54
Original
2266 people have browsed it

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

Copy after login


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

The code is as follows:

Copy after login


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

The code is as follows:

//有multiple属性,则可以多选 
 
//下面没有multiple属性 , 只显示一条,不能多选 
 
//下面是设置了size属性的情况 , 如果size = 3 那么就显示三条数据,注意不能多选的。 
Copy after login


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
Copy after login


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('提示消息','该值的选项已经存在!','info'); 
} else { 
var varItem = new Option(objItemText,objItemValue); 
objSelect.options.add(varItem); 
} 
} 
}
Copy after login


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
Copy after login


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
Copy after login


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
Copy after login


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; 
}
Copy after login


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
Copy after login


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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!