javascript之html(select option)详解
一、基础理解:
var e = document.getelementbyid(selectid);
e. options= new option(文本,值) ;
//创建一个option对象,即在221f08282418e2996498697df914ce4e标签中创建一个或多个1014af8230147c909654ea3814cbcba5文本4afa15d3069109ac30911f04c56f3338
//options是个数组,里面可以存放多个1014af8230147c909654ea3814cbcba5文本4afa15d3069109ac30911f04c56f3338这样的标签
1:options[ ]数组的属性:
length属性---------长度属性
selectedindex属性--------当前被选中的框中的文本的索引值,此索引值是内存自动分配的(0,1,2,3.....)对应(第一个文本值,第二个文本值,第三个文本值,第四个文本值..........)
2:单个option的属性(---obj.options[obj.selecedindex]是指定的某个5a07473c87748fb1bf73f23d45547ab8标签,是一个---)
text属性---------返回/指定 文本
value属性------返回/指定 值,与ce2edcfb281b522a5fec6e9ad477b306一致。
index属性-------返回下标,
selected 属性-------返回/指定该对象是否被选中.通过指定 true 或者 false,可以动态的改变选中项
defaultselected 属性-----返回该对象默认是否被选中。true / false。
3:option的方法
增加一个5a07473c87748fb1bf73f23d45547ab8标签-----obj.options.add(new(文本,值));0a38cce9d2d2b03f41185a27bf4d4838
删除一个5a07473c87748fb1bf73f23d45547ab8标签-----obj.options.remove(obj.selectedindex)c423837478d0e66dfb8dfc3b12936a88
获得一个5a07473c87748fb1bf73f23d45547ab8标签的文本-----obj.options[obj.selectedindex].textf59950d08a630f0f7b589102ed578e0f
修改一个5a07473c87748fb1bf73f23d45547ab8标签的值-----obj.options[obj.selectedindex]=new option(新文本,新值)15eb592aa96a9b42e3894d9661b4b2f7
删除所有5a07473c87748fb1bf73f23d45547ab8标签-----obj.options.length = 0
获得一个5a07473c87748fb1bf73f23d45547ab8标签的值-----obj.options[obj.selectedindex].value
注意:
a:上面的写的是如这样类型的方法obj.options.function()而不写obj.funciton,是因为为了考虑在ie和ff 下的兼容,如obj.add()只能在ie中有效.
b:obj.option中的option不需要大写,new option中的option需要大写
二 、应用
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
10a80d49cacb7ad8386f440152676a4b
function number(){
var obj = document.getelementbyid(myselect);
//obj.options[obj.selectedindex] = new option(我的吃吃,4);//在当前选中的那个的值中改变
//obj.options.add(new option(我的吃吃,4));再添加一个option
//alert(obj.selectedindex);//显示序号,option自己设置的
//obj.options[obj.selectedindex].text = 我的吃吃;更改值
//obj.remove(obj.selectedindex);删除功能
}
2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
65398704d03602dd6970669c8764bf5a
5a07473c87748fb1bf73f23d45547ab8我的包包4afa15d3069109ac30911f04c56f3338
5a07473c87748fb1bf73f23d45547ab8我的本本4afa15d3069109ac30911f04c56f3338
5a07473c87748fb1bf73f23d45547ab8我的油油4afa15d3069109ac30911f04c56f3338
5a07473c87748fb1bf73f23d45547ab8我的担子4afa15d3069109ac30911f04c56f3338
18bb6ffaf0152bbe49cd8a3620346341
d8e7143929ffe09fd1860ca0b45d0767
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
1.动态创建select
function createselect(){
var myselect = document.createelement(select);
myselect.id = myselect;
document.body.appendchild(myselect);
}
2.添加选项option
function addoption(){
//根据id查找对象,
var obj=document.getelementbyid('myselect');
//添加一个选项
obj.add(new option(文本,值)); //这个只能在ie中有效
obj.options.add(new option(text,value)); //这个兼容ie与firefox
}
3.删除所有选项option
function removeall(){
var obj=document.getelementbyid('myselect');
obj.options.length=0;
}
4.删除一个选项option
function removeone(){
var obj=document.getelementbyid('myselect');
//index,要删除选项的序号,这里取当前选中选项的序号
var index=obj.selectedindex;
obj.options.remove(index);
}
5.获得选项option的值
var obj=document.getelementbyid('myselect');
var index=obj.selectedindex; //序号,取当前选中选项的序号
var val = obj.options[index].value;
6.获得选项option的文本
var obj=document.getelementbyid('myselect');
var index=obj.selectedindex; //序号,取当前选中选项的序号
var val = obj.options[index].text;
7.修改选项option
var obj=document.getelementbyid('myselect');
var index=obj.selectedindex; //序号,取当前选中选项的序号
var val = obj.options[index]=new option(新文本,新值);
8.删除select
function removeselect(){
var myselect = document.getelementbyid(myselect);
myselect.parentnode.removechild(myselect);
}
<!doctype html public "-//w3c//dtd html 4.01//zh-cn" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html"> <head> <script language=javascript> function $(id) { return document.getelementbyid(id) } function show() { var selectobj=$("area") var myoption=document.createelement("option") myoption.setattribute("value","10") myoption.appendchild(document.createtextnode("上海")) var myoption1=document.createelement("option") myoption1.setattribute("value","100") myoption1.appendchild(document.createtextnode("南京")) selectobj.appendchild(myoption) selectobj.appendchild(myoption1) } function choice() { var index=$("area").selectedindex; var val=$("area").options[index].getattribute("value") if(val==10) { var i=$("context").childnodes.length-1; var remobj=$("context").childnodes[i]; remobj.removenode(true) var sh=document.createelement("select") sh.add(new option("浦东新区","101")) sh.add(new option("黄浦区","102")) sh.add(new option("徐汇区","103")) sh.add(new option("普陀区","104")) $("context").appendchild(sh) } if(val==100) { var i=$("context").childnodes.length-1; var remobj=$("context").childnodes[i]; remobj.removenode(true) var nj=document.createelement("select") nj.add(new option("玄武区","201")) nj.add(new option("白下区","202")) nj.add(new option("下关区","203")) nj.add(new option("栖霞区","204")) $("context").appendchild(nj) } } function calc() { var x=$("context").childnodes.length-1; alert(x) } function remove() { var i=$("context").childnodes.length-1; var remobj=$("context").childnodes[i]; remobj.removenode(true) } </script> <body> <p id="context"> <select id="area" on change="choice()"> </select> </p> <input type=button value="显示" onclick="show()"> <input type=button value="计算结点" onclick="calc()"> <input type=button value="删除" onclick="remove()"> </body> </html>
根据这些东西,自己用jqeury ajax+json实现了一个小功能如下:
js代码:(只取了于select相关的代码)
/** * @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.length;i++){ //create select option tempoption= $j('<option value="'+nodearray[i].dm+'">'+nodearray[i].mc+'</option> '); //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<length;i++) document.getelementbyid(selectid).options.remove(i); length=length/2; } } /** * @description 获取退化构件列表 * @prarm selectid1 引用软件下拉列表的id * @prarm selectid2 退化构件下拉列表的id */ function getcpgjthgl(selectid1,selectid2){ var obj1=document.getelementbyid(selectid1);//引用软件下拉列表 var obj2=document.getelementbyid(selectid2);//退化构件下拉列表 var len=obj1.options.length; //当引用软件列表长度等于1时返回,不做操作 if(len==1){ return false; } //清空下拉列表的值,两种方式都可以 // isclearselect(selectid2,'1'); document.getelementbyid(selectid2).length=1; for(var i=0;i<len; i++){ var option= obj1.options[i]; //引用软件被选中项不加入 if(i!=obj1.selectedindex){ //克隆option并添加到select中 obj2.appendchild(option.clonenode(true)); } } }
html代码:
<table width="100%" border=0 align="left" cellpadding=0 cellspacing=1> <tr> <td class="search_item_18"> <span class="edit_mustinput">*</span>引用软件:</td> <td class="search_content_82"> <input name="yyrjmc" id="yyrjmc" type="text" class="search_input" tabindex="3" size="30" > <input name="yyrjdm" id="yyrjdm" type="hidden" > <input type="button" class="search_button_select" onclick="linkagetree('linkage','yyrjtree','yyrjmc','yyrjdm','linkagetree','1');" value="选择..."> </td> </tr> <tr> <td class="search_item"> <span class="edit_mustinput">*</span>引用分版:</td> <td class="search_content" id="yyfb"> <select name="yyfbdm" style="width:160" id="yyfbdm" onchange="getcpgjthgl('yyfbdm','thgjdm')"> </select> </td> </tr> <tr> <td class="search_item">退化构件:</td> <td class="search_content" id="thgj"> <select name="thgjdm" style="width:160" id="thgjdm"> <option value="-1" selected>无</option> </select> </td> </tr> </table>
以上就是html select option详细说明的详细内容。