关于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">
[解决办法]
两者都可以!不错