参考如下代码<script language="JavaScript">
function copyToList(from,to) //from表示:包含可选择项目的select对象名字 to表示:列出可选择项目的select对象名字 //你可以根据你的具体情况修改
{
fromList = eval('document.forms[0].' + from);
toList = eval('document.forms[0].' + to);
if (toList.options.length > 0 && toList.options[0].value == 'temp')
{
toList.options.length = 0;
}
var sel = false;
for (i=0;i<fromList.options.length;i++)
{
var current = fromList.options[i];
if (current.selected)
{
sel = true;
if (current.value == 'temp')
{
alert ('你不能选择这个项目!');
return;
}
txt = current.text;
val = current.value;
toList.options[toList.length] = new Option(txt,val);
fromList.options[i] = null;
i--;
}
}
if (!sel) alert ('你还没有选择任何项目');
}
function allSelect() //这是当用户按下提交按钮时,对列出选择的select对象执行全选工作,让递交至的后台程序能取得相关数据
{
List = document.forms[0].chosen;
if (List.length && List.options[0].value == 'temp') return;
for (i=0;i<List.length;i++)
{
List.options[i].selected = true;
}
}</script>//--------------------------------------------------------------下面放到body
<table border="0"> <form onSubmit="allSelect()">
<tr>
<td>
<select name="possible" size="4"
MULTIPLE width=200 style="width: 200px">
<option value="1">中国广州
<option value="2">中国上海
<option value="3">中国北京
<option value="4">中国武汉
</select>
</td>
<td><a href="javascript:copyToList('possible','chosen')">添加至右方--><br>
<br>
</a><a href="javascript:copyToList('chosen','possible')"><--添加至左方</a></td>
<td>
<select name="chosen" size="4"
MULTIPLE width=200 style="width: 200px;">
<option value="temp">从左边选择你的地区
</select>
</td>
</tr> </form>
</table>
function copyToList(from,to) //from表示:包含可选择项目的select对象名字 to表示:列出可选择项目的select对象名字 //你可以根据你的具体情况修改
{
fromList = eval('document.forms[0].' + from);
toList = eval('document.forms[0].' + to);
if (toList.options.length > 0 && toList.options[0].value == 'temp')
{
toList.options.length = 0;
}
var sel = false;
for (i=0;i<fromList.options.length;i++)
{
var current = fromList.options[i];
if (current.selected)
{
sel = true;
if (current.value == 'temp')
{
alert ('你不能选择这个项目!');
return;
}
txt = current.text;
val = current.value;
toList.options[toList.length] = new Option(txt,val);
fromList.options[i] = null;
i--;
}
}
if (!sel) alert ('你还没有选择任何项目');
}
function allSelect() //这是当用户按下提交按钮时,对列出选择的select对象执行全选工作,让递交至的后台程序能取得相关数据
{
List = document.forms[0].chosen;
if (List.length && List.options[0].value == 'temp') return;
for (i=0;i<List.length;i++)
{
List.options[i].selected = true;
}
}</script>//--------------------------------------------------------------下面放到body
<table border="0"> <form onSubmit="allSelect()">
<tr>
<td>
<select name="possible" size="4"
MULTIPLE width=200 style="width: 200px">
<option value="1">中国广州
<option value="2">中国上海
<option value="3">中国北京
<option value="4">中国武汉
</select>
</td>
<td><a href="javascript:copyToList('possible','chosen')">添加至右方--><br>
<br>
</a><a href="javascript:copyToList('chosen','possible')"><--添加至左方</a></td>
<td>
<select name="chosen" size="4"
MULTIPLE width=200 style="width: 200px;">
<option value="temp">从左边选择你的地区
</select>
</td>
</tr> </form>
</table>
// 添加选项
function addOption(pos){
var objSelect = document.myForm.mySelect;
// 取得字段值
var strName = document.myForm.myOptionName.value;
var strValue = document.myForm.myOptionValue.value;
// 建立Option对象
var objOption = new Option(strName,strValue);
if (pos == -1 & pos > objSelect.options.length)
objSelect.options[objSelect.options.length] = objOption;
else
objSelect.add(objOption, pos);
}
// 删除选项
function deleteOption(type){
var objSelect = document.myForm.mySelect;
if(objSelect.options.length>0){
if (type == true)
objSelect.options[objSelect.selectedIndex] = null;
else
objSelect.remove(objSelect.selectedIndex);
}
else
alert("已经没有选项可以删除了!")
}
// 显示选项信息
function showOption(objForm){
var objSelect = objForm.mySelect;
if(objSelect.options.length>0){
document.all.myOptionName.value = objSelect.options[objSelect.selectedIndex].text;
document.all.myOptionValue.value = objSelect.options[objSelect.selectedIndex].value;
}
else
alert("没有选项可以显示!")
}
</script>
<form name="myForm">
<select name="mySelect">
<option value="value1" Selected>HTML</option>
<option value="value2">JavaScript</option>
<option value="value3">VBScript</option>
</select>
<input type="button" onclick="showOption(this.form)" value="显示">
<input type="button" onclick="deleteOption(true)" value="删除">
<input type="button" onclick="deleteOption(false)" value="Remove方法"><br/><br/>
选项名称 : <input type="text" name="myOptionName" value="CSS"><br/>
选项的值 : <input type="text" name="myOptionValue" value="value4">
<input type="button" onclick="addOption(-1)" value="添加">
<input type="button" onclick="addOption(0)" value="插入">
</form>
<input type="text" name="str2">
<input type="button" name="add" value="添加"
onclick="SelAdd(document.all.str1,document.all.str2)">
<select name="mySelect">
</select>
<input type="button" name="del" value="删除"
onclick="SelDel(document.all.mySelect.options.selectedIndex)">
<input type="text" name="s" value="">
<script>
function SelAdd(obj1,obj2)
{
var sel = document.all.mySelect;
var optVal = obj1.value + "," + obj2.value;
sel.options[sel.options.length] = new Option(optVal,optVal);
obj1.value = "";
obj2.value = "";
ShowOpts();
}
function SelDel(iIndex)
{
document.all.mySelect.options[iIndex] = null;
ShowOpts();
}
function ShowOpts()
{
var sel = document.all.mySelect;
var str = new Array();
for(var i=0;i<sel.options.length;i++)
{
str[str.length] = sel.options[i].value;
}
document.all.s.value = str.join(";");
}
</script>