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

javascript代码如下:

var selectTag = null; //select标记
    var OPTONLENGTH = 10; //每次填充option数
    var colls = [];       //对select标记options的引用

    window.onload = function(){
        selectTag = document.getElementById("SelectBox"); //获取select标记        
        colls = selectTag.options; //获取引用
        //initSelectBox();    //自初始化select.options
    };
    
    //使用随机数填充select.options
    function initSelectBox(){
        var random = 0 ;
        var optionItem = null;
        var item = null;
        
        if(colls.length > 0 && isClearOption()){
             clearOptions(colls);
        }

        for(var i=0;i<OPTONLENGTH;i++){
             
            random = Math.floor(Math.random()*9000)+1000;
            item = new Option(random,random);    //通过Option()构造函数创建option对象        
            selectTag.options.add(item); //添加到options集合中
        }    
        
        watchState();
    }
    //添加新option项前是否清空当前options
    function isClearOption(){
        return document.getElementById("chkClear").checked;
    }
    //清空options集合
    function clearOptions(colls){
        var length = colls.length;
        for(var i=length-1;i>=0;i--){
            colls.remove(i);
        }
    }
    //添加一项新option
    function addOption(){
        colls.add(createOption());
        lastOptionOnFocus(colls.length-1);
        watchState();
    }
    //创建一个option对象
    function createOption(){
        var random = Math.floor(Math.random()*9000)+1000;
        return new Option(random,random);
    }
    //删除options集合中指定的一项option
    function removeOption(index){        
        if(index >= 0){
            colls.remove(index);
            lastOptionOnFocus(colls.length-1);
        }
        watchState();
    }
    //获取当前选定的option索引
    function getSelectedIndex(){
        return selectTag.selectedIndex;
    }
    //获取options集合的总数
    function getOptionLength(){
        return colls.length;
    }
    //获取当前选定的option文本
    function getCurrentOptionValue(index){
        if(index >= 0)
            return colls(index).value;
    }
    //获取当前选定的option值
    function getCurrentOptionText(index){
        if(index >= 0)
            return colls(index).text;
    }
    //使用options集合中最后一项获取焦点
    function lastOptionOnFocus(index){
        selectTag.selectedIndex = index;
    }
    //显示当select标记状态
    function watchState(){
        var divWatch = document.getElementById("divWatch");
        var innerHtml="";
        innerHtml  = "option总数:" + getOptionLength();
        innerHtml += "<br/>当前项索引:" + getSelectedIndex();
        innerHtml += "<br/>当前项文本:" + getCurrentOptionText(getSelectedIndex());
        innerHtml += "<br/>当前项值:" + getCurrentOptionValue(getSelectedIndex());
        divWatch.innerHTML = innerHtml;
        divWatch.align = "justify";
    }

注意到上面创建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; //选中项的值

<BODY>
  <Select name="SelectBox">
  </Select>
  <hr/>
    <div id="divWatch" style="background-color:beige;width=220;">
    </div>    
  <hr/>
  <h4>使用随机数初始化SelectBox</h4>
  <input type="button" value="Init" onclick="initSelectBox()"/> <input type="checkbox" name="chkClear"/>clear
  <hr/>
  <h4>添加option项</h4>
  <input type="button" value="create" onclick="addOption()"/>
  <hr/>
  <h4>删除option项</h4>
  <input type="button" value="delete" onclick="removeOption(colls.length-1)"/>
 </BODY>

【转】javascript操作Select标记中options集合的更多相关文章

  1. 转 JavaScript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    收藏一下 1.判断select选项中 是否存在Value=”paraValue”的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.删除select中选中 ...

  2. Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    1判断select选项中 是否存在Value="paraValue"的Item 2向select选项中 加入一个Item 3从select选项中 删除一个Item 4删除selec ...

  3. javascript操作select元素一例

    熟悉一下js对select元素的操作,html页面中建立一个form,其中包含一个select元素和submit按钮. 当选择select中某一项时改变其文字,当select中所有项的文字都改变后,重 ...

  4. javaScript操作select

    注意:Option中的O是要大写的,不然语法报错 1.动态创建select       function createSelect(){ var mySelect = document.createE ...

  5. JavaScript操作select下拉框选项移动

    运行结果: 源代码: 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta char ...

  6. Jquery操作select选项集合,判断集合中是否存在option

    转载:http://www.cnblogs.com/pepcod/archive/2012/07/03/JavaScript.html Query获取Select选择的Text和Value: 语法解释 ...

  7. JS对select动态添加options操作[IE&FireFox兼容]

    <select id="ddlResourceType" onchange="getvalue(this)"> </select> 动态 ...

  8. jq操作select集合

    jq操作select集合 时间:2012年12月07日分类:Javascript 最近一段时间发现,老是要跟select,option相关的东西打交道,而且有的时候还会搞错,于是,抽了一点时间整理了一 ...

  9. JavaScript获取select下拉框中的第一个值

    JavaScript获取select下拉框中的第一个值 1.说明 获取select下拉框中的第一个值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

随机推荐

  1. [ros]编译ORBSLAM2时候,ros路径问题

    CMake Error at CMakeLists.txt:2 (include): include could not find load file: /core/rosbuild/rosbuild ...

  2. Python3自动化学习地址

    w3社区https://www.w3cschool.cn/python/ 易百教程https://www.yiibai.com/ http://www.cnblogs.com/yangxia-test ...

  3. checkbox 全选 单选的使用

    绑定数据 if (!IsPostBack) { using (UsersDataContext con = new UsersDataContext()) { Repeater1.DataSource ...

  4. [LR]遇到的坑及常用技巧

    解决问题是需要智慧的 1.LR内部问题可以查看帮助文档 一般位于LR安装目录的bin目录下,如下图 打开Troubleshooting.chm文件,比如我们跑场景的过程中报错,报错信息为:-27995 ...

  5. codeforce Gym 100500E IBM Chill Zone (SG函数)

    关于sg函数这篇blog讲得很详细http://blog.csdn.net/logic_nut/article/details/4711489. sg函数的价值在于把复杂的游戏拆分成简单的游戏,然后通 ...

  6. php有哪些优化技巧

    1. echo 比 print 快.2. 使用echo的多重参数代替字符串连接.3. 在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替.4. 对global变 ...

  7. cpp代码调试,调试扑克牌的代码

    #include <iostream> #include <vector> #include <algorithm> using namespace std; cl ...

  8. 绘制方式和OpenGL枚举对应关系

    绘制方式和OpenGL枚举对应关系 图元类型 OpenGL枚举量 点 GL_POINTS 线 GL_LINES 条带线 GL_LINE_STRIP 循环线 GL_LINE_LOOP 独立三角形 GL_ ...

  9. java的IO机制

    BIO.NIO.AIO -----> Block-IO : inputStream 和OutputStream,Reader和Writer 1个连接,启动一个线程,这样导致很大的线程开销 NIO ...

  10. Bootstrap历练实例:标签修饰

    您可以使用修饰的 class label-default.label-primary.label-success.label-info.label-warning.label-danger 来改变标签 ...