summoner好酷哦,收藏
这个带有验证是否有选中项的函数
[CODE]<html>
<head>
<title></title>
</head>
<script language="JavaScript">
function addnew(){
if(checked_check("s1") != true)
{
alert("Please select a option!");
return false;
}
var t=document.all.s1;
var tt=document.all.s2;
var text=t.options[t.selectedIndex].innerText;
var value=t.options[t.selectedIndex].value;
tt.options.add(new Option(text,value));
t.options.remove(t.selectedIndex);
t.selectedIndex = 0;
tt.selectedIndex = 0;
}
function remove(){
if(checked_check("s2") != true)
{
alert("Please select a option!");
return false;
}
var t1=document.all.s1;
var t2=document.all.s2;
var text=t2.options[t2.selectedIndex].innerText;
var value=t2.options[t2.selectedIndex].value;
s2.options.remove(t2.selectedIndex);
t1.options.add(new Option(text,value));
s1.selectedIndex = 0;
s2.selectedIndex = 0;
}
function checked_check(idname)
{
var checkobject = document.getElementById(idname);
var coun = checkobject.length;
for(i=0;i<coun;i++)
{
if(checkobject.options[i].selected = true)
return true;
}
return false;
}
</script>
<body>
<select size=3 id=s1>
<option value=a selected >a_________</option>
<option value=b>b________</option>
<option value=c>c__</option>
<option value=d>d________</option>
</select>
<input type=button onclick=addnew() value=add>
<input type=button onclick=remove() value=delete>
<select size=3 id=s2></select>
</body>
</html>[/CODE]