日期:2014-05-16  浏览次数:20345 次

关于select 插值问题 在线坐等大神求助!!!急!!!
背景介绍:
  向 select中插入新值 如何通过js 将新值 放入到select的最上端  


如:插入前
111
222
333
444

插入后
888
111
222
333
444

------解决方案--------------------
HTML code
<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()">

------解决方案--------------------
<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">

------解决方案--------------------
两者都可以!不错