js select option对象

本文介绍如何使用JavaScript操作HTML中的Select元素,包括添加、删除选项及动态修改选项值等,并通过实例展示了如何利用jQuery AJAX配合JSON实现级联下拉列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一基础理解:

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

e. options= new Option("文本","值") ;

//创建一个option对象,即在<select>标签中创建一个或多个<option value="值">文本</option>

//options是个数组,里面可以存放多个<option value="值">文本</option>这样的标签

1:options[ ]数组的属性:

length属性---------长度属性

selectedIndex属性--------当前被选中的框中的文本的索引值,此索引值是内存自动分配的(0,1,2,3.....)对应(第一个文本值,第二个文本值,第三个文本值,第四个文本值..........)

2:单个option的属性(---obj.options[obj.selecedIndex]是指定的某个<option>标签,是一个---)

text属性---------返回/指定 文本

value属性------返回/指定 值,与<options value="...">一致。

index属性-------返回下标,

selected 属性-------返回/指定该对象是否被选中.通过指定 true 或者 false,可以动态的改变选中项

defaultSelected 属性-----返回该对象默认是否被选中。true / false。

3:option的方法

增加一个<option>标签-----obj.options.add(new("文本","值"));<增>

删除一个<option>标签-----obj.options.remove(obj.selectedIndex)<删>

获得一个<option>标签的文本-----obj.options[obj.selectedIndex].text<查>

修改一个<option>标签的值-----obj.options[obj.selectedIndex]=new Option("新文本","新值")<改>

删除所有<option>标签-----obj.options.length = 0

获得一个<option>标签的值-----obj.options[obj.selectedIndex].value

注意:

a:上面的写的是如这样类型的方法obj.options.function()而不写obj.funciton,是因为为了考虑在IE和FF 下的兼容,如obj.add()只能在IE中有效.

b:obj.option中的option不需要大写,new Option中的Option需要大写

二 应用 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <html>   
  2. <head>   
  3. <script language="javascript">   
  4. function number(){   
  5. var obj = document.getElementById("mySelect");   
  6.     //obj.options[obj.selectedIndex] = new Option("我的吃吃","4");//在当前选中的那个的值中改变   
  7.     //obj.options.add(new Option("我的吃吃","4"));再添加一个option   
  8.     //alert(obj.selectedIndex);//显示序号,option自己设置的   
  9.     //obj.options[obj.selectedIndex].text = "我的吃吃";更改值   
  10.    //obj.remove(obj.selectedIndex);删除功能   
  11. }   
  12. </script>   
  13. </head>   
  14. <body>   
  15. <select id="mySelect">   
  16.      <option>我的包包</option>   
  17.      <option>我的本本</option>   
  18.      <option>我的油油</option>   
  19.      <option>我的担子</option>   
  20. </select>   
  21. <input type="button" name="button" value="查看结果" onclick="number();">   
  22. </body>   
  23. </html>   

根据这些东西,自己用JQEURY AJAX+JSON实现了一个小功能如下:

JS代码:(只取了于SELECT相关的代码) 

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.    * @description  构件联动下拉列表 (用JQUERY 的AJAX配合JSON实现)  
  3.    * @prarm  selectId 下拉列表的ID  
  4.    * @prarm  method  要调用的方法名称  
  5.    * @prarm  temp 此处存放软件ID  
  6.    * @prarm  url  要跳转的地址  
  7.    */   
  8. function  linkAgeJson(selectId,method,temp,url){      
  9.       $j.ajax({       
  10.             type: "get",//使用get方法访问后台   
  11.             dataType: "json",//返回json格式的数据   
  12.             url: url,//要访问的后台地址   
  13.             data: "method=" + method+"&temp="+temp,//要发送的数据           
  14.             success: function(msg){//msg为返回的数据,在这里做数据绑定   
  15.                 var data = msg.lists;   
  16.                 coverJsonToHtml(selectId,data);                
  17.             }   
  18.         });   
  19. }  
  20. /**  
  21. * @description  将JSON数据转换成HTML数据格式  
  22. * @prarm selectId 下拉列表的ID  
  23. * @prarm  nodeArray 返回的JSON数组  
  24.  
  25. */   
  26. function coverJsonToHtml(selectId,nodeArray){   
  27. //get select   
  28.    var tempSelect=$j("#"+selectId);   
  29.    //clear select value   
  30.    isClearSelect(selectId,'0');      
  31. var tempOption=null;   
  32. for(var i=0;i<nodeArray.length;i++){   
  33. //create select Option   
  34. tempOption= $j('<option value="'+nodeArray[i].dm+'">'+nodeArray[i].mc+'</option> ');   
  35. //put Option to select   
  36. tempSelect.append(tempOption);   
  37.         }   
  38.         // 获取退化构件列表   
  39.        getCpgjThgl(selectId,'thgjDm');   
  40.    }   
  41.    /**  
  42.    * @description  清空下拉列表的值  
  43.    * @prarm selectId 下拉列表的ID  
  44.    * @prarm index 开始清空的下标位置  
  45.    */   
  46.   function isClearSelect(selectId,index){   
  47.      var length=document.getElementById(selectId).options.length;   
  48. while(length!=index){   
  49.       //长度是在变化的,因为必须重新获取    
  50.           length=document.getElementById(selectId).options.length;   
  51.           for(var i=index;i<length;i++)   
  52.              document.getElementById(selectId).options.remove(i);   
  53.          length=length/2;   
  54.      }   
  55.    }   
  56.   
  57. /**  
  58. * @description 获取退化构件列表  
  59. * @prarm  selectId1 引用软件下拉列表的ID  
  60. * @prarm  selectId2 退化构件下拉列表的ID  
  61. */   
  62.    function getCpgjThgl(selectId1,selectId2){   
  63.    var obj1=document.getElementById(selectId1);//引用软件下拉列表   
  64.    var obj2=document.getElementById(selectId2);//退化构件下拉列表   
  65.    var len=obj1.options.length;   
  66.   //当引用软件列表长度等于1时返回,不做操作   
  67.    if(len==1){   
  68.           return false;   
  69.    }   
  70.    //清空下拉列表的值,两种方式都可以   
  71.   // isClearSelect(selectId2,'1');    
  72.             document.getElementById(selectId2).length=1;   
  73.    for(var i=0;i<len; i++){   
  74. var option= obj1.options[i];    
  75. //引用软件被选中项不加入   
  76. if(i!=obj1.selectedIndex){   
  77. //克隆OPTION并添加到SELECT中     
  78. obj2.appendChild(option.cloneNode(true));   
  79. }    
  80. }  
  81.    }    

HTML代码: 

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <TABLE width="100%" border=0 align="left" cellPadding=0 cellSpacing=1>   
  2.   <tr>   
  3. <td  class="Search_item_18">  <span class="Edit_mustinput">*</span>引用软件:</td>   
  4. <td  class="Search_content_82">   
  5. <input name="yyrjMc" id="yyrjMc" type="text" class="Search_input" tabindex="3"  size="30" >   
  6. <input name="yyrjDm" id="yyrjDm" type="hidden" >   
  7. <input type="button" class="Search_button_select"   
  8. onClick="linkAgeTree('linkage','yyrjtree','yyrjMc','yyrjDm','linkageTree','1');" value="选择...">   
  9. </td>   
  10.   </tr>   
  11.   <tr>   
  12. <td class="Search_item"> <span class="Edit_mustinput">*</span>引用分版:</td>   
  13. <td  class="Search_content" id="yyfb">   
  14.   <select name="yyfbDm" style="width:160" id="yyfbDm" onChange="getCpgjThgl('yyfbDm','thgjDm')">  
  15.   </select>   
  16. </td>   
  17.   </tr>   
  18.   <tr>   
  19. <td class="Search_item">退化构件:</td>   
  20. <td  class="Search_content" id="thgj">   
  21.    <select name="thgjDm" style="width:160" id="thgjDm">   
  22. <option value="-1" selected></option>   
  23.    </select>   
  24. </td>   
  25.   </tr>   
  26. </TABLE>   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值