背景介绍:
     向 select中插入新值 如何通过js  将新值  放入到select的最上端  
如:插入前
111
222
333
444插入后
888
111
222
333
444

解决方案 »

  1.   

    <script>
    function AddSelect(){
          var selectbox=document.getElementById('sel');
      var newOption=new Option('c','c');
       selectbox.add(newOption,undefined);
      selectbox.insertBefore(newOption,selectbox.options[0]);
      selectbox.selectedIndex=0;
    }
    </script>
    <select id="sel">
    <option value='a'>a</option>
    <option value='b'>b</option>
    </select>
    <input type="button" value="Add" onclick="AddSelect()">
      

  2.   

    <script type="text/javascript">
    function add(){
    var a=new Option("4","4");
    var se=document.getElementById("test");
    var f=se.firstChild;
    se.insertBefore(a,f);
    se.selectedIndex=0;
    }
    </script>
    </head><body>
    <select id="test">
    <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
    <input type="button" onclick="add()" value="add">
      

  3.   

    上面是用对象的方式,我经常用插入HTML的方式,
    第一步:获取select的所有 HTML
    第二步:把你要插入的HTML+上一步获取到的HTML
    第三步:再把这块HTML绑到select上面