HTML select option detailed description

高洛峰
Release: 2017-03-13 18:18:17
Original
2006 people have browsed it

This article introduces the detailed explanation of HTML select option

javascriptThe detailed explanation of HTML (select option)

1. Basic understanding:

var e =document.getElementById("selectId");

e. options=newOption("text","value");

//Create an optionobject, that is, create one or more

/ in the
< ;option>My bag




onclick="number() ;">



1. Dynamically create select

function createSelect(){

var mySelect = document.createElement("select");
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}

2 .Add option option

function addOption(){

//Find the object based on id,
var obj=document.getElementById('mySelect');

/ /Add an option
obj.add(new Option("text","value")); //This is only valid in IE
obj.options.add(new Option("text"," value")); //This is compatible with IE and firefox
}

3. Delete all options option

function removeAll(){
var obj=document.getElementById('mySelect');

obj.options.length=0;

}

4. Delete an option option

function removeOne(){
var obj=document.getElementById('mySelect');

//index, the serial number of the option to be deleted, take here The serial number of the currently selected option

var index=obj.selectedIndex;
obj.options.remove(index);
}

5. Get the value of option

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

var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option

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

6. Get the text of option option

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

var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option

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

7. Modify the option option

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

var index=obj.selectedIndex; //Serial number, take the serial number of the currently selected option

var val = obj.options[index]=new Option(" New text","new value");

8. Delete select

function removeSelect(){
var mySelect = document.getElementById("mySelect");
mySelect .parentNode.removeChild(mySelect);
}

       

Copy after login


Based on these things, I used JQEURY AJAX+JSONto implement a small function as follows:

JS code: (only the code related to SELECT is taken)

/** * @description 构件联动下拉列表 (用JQUERY 的AJAX配合JSON实现) * @prarm selectId 下拉列表的ID * @prarm method 要调用的方法名称 * @prarm temp 此处存放软件ID * @prarm url 要跳转的地址 */ function linkAgeJson(selectId,method,temp,url){ $j.ajax({ type: "get",//使用get方法访问后台 dataType: "json",//返回json格式的数据 url: url,//要访问的后台地址 data: "method=" + method+"&temp="+temp,//要发送的数据 success: function(msg){//msg为返回的数据,在这里做数据绑定 var data = msg.lists; coverJsonToHtml(selectId,data); } }); } /** * @description 将JSON数据转换成HTML数据格式 * @prarm selectId 下拉列表的ID * @prarm nodeArray 返回的JSON数组 * */ function coverJsonToHtml(selectId,nodeArray){ //get select var tempSelect=$j("#"+selectId); //clear select value isClearSelect(selectId,'0'); var tempOption=null; for(var i=0;i'+nodeArray[i].mc+' '); //put Option to select tempSelect.append(tempOption); } // 获取退化构件列表 getCpgjThgl(selectId,'thgjDm'); } /** * @description 清空下拉列表的值 * @prarm selectId 下拉列表的ID * @prarm index 开始清空的下标位置 */ function isClearSelect(selectId,index){ var length=document.getElementById(selectId).options.length; while(length!=index){ //长度是在变化的,因为必须重新获取 length=document.getElementById(selectId).options.length; for(var i=index;i
        
Copy after login



##HTML code:

*引用软件:
*引用分版:
退化构件:
Copy after login


The above is the detailed content of HTML select option detailed description. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Issues
Chrome doesn't apply padding to select options Select option padding not working in Chrome