惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 水冰月

不要认为停留在心灵的舒适区域 成功不需要时间 只要你坚持 淘宝 世界名校的课件[转校内] 标题 七个受用一生的心理寓言 一辈子都有用的的75句话 完美的自己 一生中无能为力的10件事 xsd验证xml 浅谈asp.net2.0验证控件的安全使用 Javascript脚本和asp.net验证控件同时验证的代码 远程桌面“中断”巧解决 当DataSet为空时也显示GridView的表头(vs05) - 水冰月 - 博客园 收集一些.NET开发资源站点和部分优秀.NET开源项目 XP实用技巧:恢复"显示桌面"按钮 在IE7.0中如何设置代理 Win 2003 Server重新安装Oracle9i 在windows xp环境下如何完全卸载 oracle9i
javascript操作Select标记中options集合
水冰月 · 2008-12-26 · via 博客园 - 水冰月

先来看看options集合的这几个方法:
options.add(option)方法向集合里添加一项option对象;
options.remove(index)方法移除options集合中的指定项;
options(index)或options.item(index)可以通过索引获取options集合的指定项;

javascript代码如下:

 1var selectTag = null//select标记
 2    var OPTONLENGTH = 10//每次填充option数
 3    var colls = [];       //对select标记options的引用
 4
 5    window.onload = function(){
 6        selectTag = document.getElementById("SelectBox"); //获取select标记        
 7        colls = selectTag.options; //获取引用
 8        //initSelectBox();    //自初始化select.options
 9    }
;
10    
11    //使用随机数填充select.options
12    function initSelectBox(){
13        var random = 0 ;
14        var optionItem = null;
15        var item = null;
16        
17        if(colls.length > 0 && isClearOption()){
18             clearOptions(colls);
19        }

20
21        for(var i=0;i<OPTONLENGTH;i++){
22             
23            random = Math.floor(Math.random()*9000)+1000;
24            item = new Option(random,random);    //通过Option()构造函数创建option对象        
25            selectTag.options.add(item); //添加到options集合中
26        }
    
27        
28        watchState();
29    }

30    //添加新option项前是否清空当前options
31    function isClearOption(){
32        return document.getElementById("chkClear").checked;
33    }

34    //清空options集合
35    function clearOptions(colls){
36        var length = colls.length;
37        for(var i=length-1;i>=0;i--){
38            colls.remove(i);
39        }

40    }

41    //添加一项新option
42    function addOption(){
43        colls.add(createOption());
44        lastOptionOnFocus(colls.length-1);
45        watchState();
46    }

47    //创建一个option对象
48    function createOption(){
49        var random = Math.floor(Math.random()*9000)+1000;
50        return new Option(random,random);
51    }

52    //删除options集合中指定的一项option
53    function removeOption(index){        
54        if(index >= 0){
55            colls.remove(index);
56            lastOptionOnFocus(colls.length-1);
57        }

58        watchState();
59    }

60    //获取当前选定的option索引
61    function getSelectedIndex(){
62        return selectTag.selectedIndex;
63    }

64    //获取options集合的总数
65    function getOptionLength(){
66        return colls.length;
67    }

68    //获取当前选定的option文本
69    function getCurrentOptionValue(index){
70        if(index >= 0)
71            return colls(index).value;
72    }

73    //获取当前选定的option值
74    function getCurrentOptionText(index){
75        if(index >= 0)
76            return colls(index).text;
77    }

78    //使用options集合中最后一项获取焦点
79    function lastOptionOnFocus(index){
80        selectTag.selectedIndex = index;
81    }

82    //显示当select标记状态
83    function watchState(){
84        var divWatch = document.getElementById("divWatch");
85        var innerHtml="";
86        innerHtml  = "option总数:" + getOptionLength();
87        innerHtml += "<br/>当前项索引:" + getSelectedIndex();
88        innerHtml += "<br/>当前项文本:" + getCurrentOptionText(getSelectedIndex());
89        innerHtml += "<br/>当前项值:" + getCurrentOptionValue(getSelectedIndex());
90        divWatch.innerHTML = innerHtml;
91        divWatch.align = "justify";
92    }

93

注意到上面创建option项时,使用了Option()构造函数,这个构造函数有两个版本的重载。
1、var option = new Option(text,value); //这里要大写Option()
2、var option = new Option();
       option.text = text;
       option.value=value;
我个人比较喜欢第一种方法来创建option对象。
另外,select标记还有一个比较有用的属性就是selectedIndex,通过它可能获取当前选择的option索引,或通过索引设置指定options集合中哪一项被选择。
   select.selctedIndex = select.options.length-1; //将options集合中最后一项选中
   var selectedItem = select.options(select.selectedIndex);//获取当前选中项
   selectedItem.text; //选中项的文本
   selectedItem.value; //选中项的值

Code