怎样动态的从一个select框将数据移到另一个select框?

x86 2003-01-14 06:48:39
左边是未加入用户列表,右边是已加入用户列表,如何用javascript实现点一个按钮将左边所选的移到右边?

删除可以这样:
var index=document.form1.select1.selectIndex;
document.form1.select1.remove(index);

可是加入不行:
var newOption = document.createElement("OPTION");
newOption.value="aaa";
document.form1.select2.add(newOption, 1);

请问是哪里出问题了呢?我需要支持mozilla 1.x
...全文
39 8 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
x86 2003-01-15
to emu: mozilla 1.2.1测试通过,代码很好,非常感谢

to net_lover & genuis: 同样非常感谢
  • 打赏
  • 举报
回复
emu 2003-01-15
呵呵,genuis(不死鸟) 从哪里弄来的这么长的代码啊(300多行 :-P)?好象掉了几行:表单的定义找不到了,运行不下去,大概你自己也没有仔细看过吧?
  • 打赏
  • 举报
回复
孟子E章 2003-01-14
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript">
<!--
function addSrcToDestList() {
destList = window.document.forms[0].destList;
srcList = window.document.forms[0].srcList;
var len = destList.length;
for(var i = 0; i < srcList.length; i++) {
if ((srcList.options[i] != null) && (srcList.options[i].selected)) {

var found = false;
for(var count = 0; count < len; count++) {
if (destList.options[count] != null) {
if (srcList.options[i].text == destList.options[count].text) {
found = true;
break;
}
}
}
if (found != true) {
destList.options[len] = new Option(srcList.options[i].text);
len++;
}
}
}
}

function deleteFromDestList() {
var destList = window.document.forms[0].destList;
var len = destList.options.length;
for(var i = (len-1); i >= 0; i--) {
if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
destList.options[i] = null;
}
}
}
// -->
</SCRIPT>
</head>
<body>
<center>
<form method="POST">
<table bgcolor="#FFFFCC">

<tr>
<td bgcolor="#FFFFCC" width="85">
<select size="6" name="srcList" multiple>
<option value="1">Item 1
<option value="2">Item 2
<option value="3">Item 3
<option value="4">Item 4
<option value="5">Item 5
<option value="6">Item 6
</select>
</td>
<td bgcolor="#FFFFCC" width="74" align="center">
<input type="button" value=" 增加到右边 " onClick="javascript:addSrcToDestList()">
<br><br>
<input type="button" value=" 从右边删除 " onclick="javascript:deleteFromDestList();">
</td>
<td bgcolor="#FFFFCC" width="69">
<select size="6" name="destList" multiple>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
  • 打赏
  • 举报
回复
genuis 2003-01-14
//MODIFIED BY DAVID AND CLOUD
function DeleteItem(ObjName)
{ ObjID = GetObjID(ObjName);
if ( ObjID != -1 )
{ var check_index = new Array();
for (i=window.Form1.elements[ObjID].length-1; i>=0; i--)
{ if (window.Form1.elements[ObjID].options[i].selected)
{ check_index[i] = true;
window.Form1.elements[ObjID].options[i].selected = false;
}
else
check_index[i] = false;
}
for (i=window.Form1.elements[ObjID].length-1; i>=0; i--)
{ if (check_index[i])
{ info = window.Form1.elements[ObjID].options[i].value;
if (!(info.substring(2)=="00")) window.Form1.elements[ObjID].options[i] = null
}
}
var b=0
var indexarr = new Array()
for (i=0;i<window.Form1.elements[ObjID].length;i++)
{ value1 = window.Form1.elements[ObjID].options[i].value
if (value1.substring(2)=="00"){
if (window.Form1.elements[ObjID].length==i+1)
window.Form1.elements[ObjID].options[i]=null
else
{ value2 = window.Form1.elements[ObjID].options[i+1].value
if (value2.substring(2)=="00"){
indexarr[b]=i
b++
}
}
}
}
for (var c=0;c<b;c++){
window.Form1.elements[ObjID].options[indexarr[c]-c]=null
}
}
}


function IsSelected(ID, Value)
{ if (ID != -1 && Value != "")
{ for (var cc=0; cc<document.Form1.elements[ID].length; cc++ )
{ if (document.Form1.elements[ID].options[cc].value == Value)
return true;
}
return false;
}
return false;
}

function HasTotal(ID)
{ if (ID != -1 )
{ for (var cc=0; cc<document.Form1.elements[ID].length; cc++ )
{ if ( document.Form1.elements[ID].options[cc].selected )
{ if (document.Form1.elements[ID].options[cc].value == "0000")
{ return true;
}
}
}
return false;
}
return false;
}


function AppendItem(ObjName, DesName)
{ ObjID = GetObjID(ObjName);
DesObjID = GetObjID(DesName);
if (ObjID != -1 && DesObjID != -1)
{ if ( IsSelected(DesObjID, "0000") )
window.alert("已包含");
else
{ if ( HasTotal(ObjID) )
{ document.Form1.elements[DesObjID].length = 0;
document.Form1.elements[DesObjID].options[0]= new Option("---不限---", "0000");

}
else
{ if (document.Form1.elements[DesObjID].length == 10)
window.alert("最多选十项。");
else
{ //GET SELECTED ITEM NUMBER
SelNum = 0;
for (var j=0; j<document.Form1.elements[ObjID].length; j++)
{ if ( document.Form1.elements[ObjID].options[j].selected)
SelNum ++;
}
if ((SelNum + document.Form1.elements[DesObjID].length) > 10)
window.alert("最多选十项。");
else
{ //add
for (j=0; j<document.Form1.elements[ObjID].length; j++)
{ if (document.Form1.elements[ObjID].options[j].selected)
{ //GET VALUE
dd = document.Form1.elements[ObjID].options[j].value;
if (!IsSelected(DesObjID, dd))
{ //GET LENGTH
DesLen = document.Form1.elements[DesObjID].length;
// NEW OPTION
document.Form1.elements[DesObjID].options[DesLen]= new Option(document.Form1.elements[ObjID].options[j].text, document.Form1.elements[ObjID].options[j].value);
}
else
window.alert("此选项已选择。");

}
}
}
}
}
}
//CLEAR
for (j=0; j<document.Form1.elements[ObjID].length; j++)
document.Form1.elements[ObjID].options[j].selected = false;
}
}

function RemoveItem(ObjName)
{ ObjID = GetObjID(ObjName);
if ( ObjID != -1 )
{ var check_index = new Array();
for (i=window.Form1.elements[ObjID].length-1; i>=0; i--)
{ if (window.Form1.elements[ObjID].options[i].selected)
{ check_index[i] = true;
window.Form1.elements[ObjID].options[i].selected = false;
}
else
check_index[i] = false;
}
for (i=window.Form1.elements[ObjID].length-1; i>=0; i--)
{ if (check_index[i])
window.Form1.elements[ObjID].options[i] = null
}
}
}

</SCRIPT>

<input type="hidden" name="madename" value="">   双击可增加试题
</td>
<td width="32%" height="42" align="center"></td>
</tr>
<tr>
<td width="12%" height="18"></td>
<td width="35%" height="18" align="center"></td>
<td width="21%" height="18" align="center"></td>
<td width="32%" height="18" align="center"></td>
</tr>
<tbody>
<tr>
<td width="12%" height="271"><img height=11
src="../images/lock_off.gif" width=19>单项选择题:</td>
<td width="35%" height="271" align="center"> <select name="select" size="20" style="WIDTH: 160px" ondblclick="JavaScript:AppendItem('select','SelectedChoice')">

<option value="16">HTTP协议是什么意思?</option>

<option value="15">个人PC中,用来储存大量数据的介质叫什么?</option>

<option value="14">1MB等于多少KB?</option>

</select> </td>
<td width="21%" height="271" align="center"> <input name="Input" onClick="JavaScript:AppendItem('select','SelectedChoice')" type=button value="添加 >>">
<br> <br> <br> <input name="Input" onClick="JavaScript:RemoveItem('SelectedChoice')" type=button value="<< 删除">
</td>
<td width="32%" height="271" align="center"> <select multiple name=SelectedChoice size=10
style="WIDTH: 160px">
</select> </td>
  • 打赏
  • 举报
回复
genuis 2003-01-14

<SCRIPT language=JAVASCRIPT>
function SelectTotal(ObjName)
{ ObjID = GetObjID(ObjName);
if (ObjID != -1)
{ for (i=0; i<document.Form1.elements[ObjID].length; i++)
document.Form1.elements[ObjID].options[i].selected = true;
}
}

function GetObjID(ObjName)
{ for ( var ObjID=0; ObjID < window.Form1.elements.length; ObjID++ )
if (window.Form1.elements[ObjID].name == ObjName )
{ return(ObjID);
break;
}
return(-1);
}

function ChangeOption(ObjName, DesName)
{
//GET OBJECT VALUE, ID OF DESTINATION OBJECT
ObjID = GetObjID(ObjName);
DesObjID = GetObjID(DesName);
//CLEAR OLD OPTIONS
document.Form1.elements[DesObjID].length= 0;
//Add NEW OPTIONS
if ( ObjID != -1 && DesObjID != -1 )
{ CatValue = window.Form1.elements[ObjID].value.substring(0, 2);
if ( CatValue == "00" )
document.Form1.elements[DesObjID].length=0;
else
{ //PARSING
if ( ObjName == "AreaCat")
var tt = document.Form1.AreaCode.value;
else
var tt = document.Form1.FuncCode.value;
var SubCategory = tt.split(";");
//GENERATE OPTIONS
j=0;
for (var i=0; i< SubCategory.length; i++ )
{ pp = SubCategory[i].split(",");
if ( pp[0].substring(0,2) == CatValue && pp[0].substring(2,4) != "00" )
{ document.Form1.elements[DesObjID].options[j]= new Option( pp[1], pp[0]);
j++;
}//if
}//for
}//if
}//if
}//function

function InsertItem(ObjID, Location)
{ len=document.Form1.elements[ObjID].length;
for (counter=len; counter>Location; counter--)
{ Value = document.Form1.elements[ObjID].options[counter-1].value;
Text2Insert = document.Form1.elements[ObjID].options[counter-1].text;
document.Form1.elements[ObjID].options[counter] = new Option(Text2Insert, Value);
}
}

function GetLocation(ObjID, Value)
{ total=document.Form1.elements[ObjID].length;
for (pp=0; pp<total; pp++)
if (document.Form1.elements[ObjID].options[pp].text == "---"+Value+"---")
{ return (pp);
break;
}
return (-1);
}

function AddItem(ObjName, DesName, CatName)
{
//GET OBJECT ID AND DESTINATION OBJECT
ObjID = GetObjID(ObjName);
DesObjID = GetObjID(DesName);
CatObjID = GetObjID(CatName);

if ( ObjID != -1 && DesObjID != -1 && CatObjID != -1 )
{ hasNum = overalert(DesObjID);
if (hasNum == 5)
window.alert("最多选五项。");
else
{ //GET SELECTED ITEM NUMBER
SelNum = 0;
for (var j=0; j<document.Form1.elements[ObjID].length; j++)
{ if ( document.Form1.elements[ObjID].options[j].selected)
SelNum ++;
}
if ((SelNum + hasNum) > 5)
window.alert("最多选五项。");
else
{ jj = document.Form1.elements[CatObjID].selectedIndex;
CatValue = document.Form1.elements[CatObjID].options[jj].text;
CatCode = document.Form1.elements[CatObjID].options[jj].value;
i = document.Form1.elements[ObjID].options.length;
j = document.Form1.elements[DesObjID].options.length;
for (h=0; h<i; h++)
{ if (document.Form1.elements[ObjID].options[h].selected )
{ Code = document.Form1.elements[ObjID].options[h].value;
Text = document.Form1.elements[ObjID].options[h].text;
j = document.Form1.elements[DesObjID].options.length;

HasSelected = false;
for (k=0; k<j; k++ )
if (document.Form1.elements[DesObjID].options[k].value == Code)
{ HasSelected = true;
break;
}
if (HasSelected == false)
{ Location = GetLocation(DesObjID, CatValue);
if (Location == -1 )
{ document.Form1.elements[DesObjID].options[j] = new Option("---"+CatValue+"---",CatCode);
document.Form1.elements[DesObjID].options[j+1] = new Option(Text, Code);
}
else
{ InsertItem(DesObjID, Location+1);
document.Form1.elements[DesObjID].options[Location+1] = new Option(Text, Code);
}
}
else
window.alert("此项已选择。");
}
}
}//else
}//else
//CLEAR SELECTION
for (i=0; i<document.Form1.elements[ObjID].length; i++)
document.Form1.elements[ObjID].options[i].selected = false;

}//if
}//end of function

//written by liming
function overalert(ID)
{ var over = 0;
thelength = document.Form1.elements[ID].length;
for (m=0; m<thelength; m++)
{ thevalue = document.Form1.elements[DesObjID].options[m].value
thevalue = thevalue.substring(2)
if (thevalue!="00")
over=over+1;
}
return(over);
}
//end

  • 打赏
  • 举报
回复
emu 2003-01-14
IE5,NS6.2通过,MOZ没有试过,应该也可以的:

<HTML>
<HEAD>
<TITLE> emu </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function exchange(v){
elm1=document.getElementsByName("s1")[v];
elm2=document.getElementsByName("s1")[1-v];
for (var i=elm1.length-1;i>-1;i--){
op=elm1.options[i];
if (op.selected){
elm2.options[elm2.length]=new Option(op.text,op.value);
elm1.options[i]=null;
}
}
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<select size=10 name=s1 style="width:100" multiple>
<option>test0</option>
<option>test1</option>
<option>test2</option>
<option>test3</option>
<option>test4</option>
<option>test5</option>
<option>test6</option>
<option>test7</option>
<option>test8</option>
<option>test9</option>
</select>
<select size=10 name=s1 style="width:100" multiple></select>
<BR>      
<input type=button value=">>" onclick="exchange(0)">          
<input type=button value="<<" onclick="exchange(1)">
</BODY>
</HTML>
  • 打赏
  • 举报
回复
zykj_2000 2003-01-14
试试不就知道了,
  • 打赏
  • 举报
回复
x86 2003-01-14
知道了,document.form1.select2.add(newOption, null);
就可以了。

不知道对不对
  • 打赏
  • 举报
回复
相关推荐
发帖
JavaScript

8.6w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2003-01-14 06:48
社区公告
暂无公告