HTML select的上移下移该如何写算法?
<select size= "10 " multiple id= "p1 ">
<option> a </option>
<option> b </option>
<option> c </option>
</select>
<input type= "button " value= "上移 ">
<input type= "button " value= "下移 ">
上移下移的算法该如何实现?
------解决方案-------------------- <script language= "javascript " type= "text/javascript ">
function up()
{
var count=getId( "p1 ").options.length;
var m=getId( "p1 ").selectedIndex
if(0==m)
{
alert( "已经是第一项了 ");
return
}
for(var i=0;i <count;i++)
getId( "p1 ").options[i].selected =false;
getId( "p1 ").options[m-1].selected =true;
}
function getId(id)
{return document.getElementById(id);}
function next()
{
var count=getId( "p1 ").options.length;
var m=getId( "p1 ").selectedIndex
if(count==m+1)
{
alert( "已经是最后一项了 ");
return
}
for(var i=0;i <count;i++)
getId( "p1 ").options[i].selected =false;
getId( "p1 ").options[m+1].selected =true;
}
</script>
<select size= "10 " multiple id= "p1 ">
<option> a </option>
<option> b </option>
<option> c </option>
</select>
<input type= "button " value= "上移 " onclick= "up() ">
<input type= "button " value= "下移 " onclick= "next() ">
------解决方案--------------------以前好像有过这类的问题,
在js版区打一下吧
------解决方案-------------------- <select name=s1 multiple size=4>
<option value=1> aaaaaa
<option value=2> bbbbbb
<option value=3> cccccc
<option value=4> dddddd
<option value=5> eeeeee
<option value=6> ffffff
<option value=7> gggggg
<option value=8> hhhhhh
</select>
<input type=button name=b1 value=向上 onClick= "up() ">
<input type=button name=b2 value=向下 onClick= "down() ">
<input type=button name=b3 value=查看 onClick= "show() ">
<script>
function up() {
s = document.all.s1;
v = new Array();
for(i=0;i <s.length-1;i++) {
if(! s.options[i].selected && s.options[i+1].selected) {
v.value = s.options[i].value;
v.text = s.options[i].text;
v.selected = s.options[i].selected;
s.options[i].value = s.options[i+1].value;
s.options[i].text = s.options[i+1].text;
s.options[i].selected = s.options[i+1].selected;
s.options[i+1].value = v.value;
s.options[i+1].text = v.text;
s.options[i+1].selected = v.selected;
}
}
}
function down() {
s = document.all.s1;
v = new Array();
for(i=s.length-1;i> 0;i--)