<script language="javascript">
function add_singer(object,value,text)//添加数据
{
if(singer_exist(object,value)==false)
{
object.options.add(new Option(text,value,true,true));
return true;
}
return false;
}
function remove_singer(object,index)//删除数据
{
if(index<0) return false;
object.options.remove(index)
}
function singer_exist(object,value)//检查是否存在
{
for(var i=0;i<object.options.length; i++)
{
if(object.options[i].value==value)
return true;
}
return false;
}
function add()
{
var obj1=document.all.list1;
var index=obj1.selectedIndex;
if (index<0) return false;
value=obj1.options[index].value;
text=obj1.options[index].text;
var obj2=document.all.list2;
add_singer(obj2,value,text)
remove_singer(obj1,index)
}function del()
{
var obj2=document.all.list2;
var index=obj2.selectedIndex;
if (index<0) return false;
value=obj2.options[index].value;
text=obj2.options[index].text;
var obj1=document.all.list1;
add_singer(obj1,value,text)
remove_singer(obj2,index)
}
</script><table>
<tr align=center>
<td width=160>
<select size=10 name="list1" style="width:120">
<option value=1>1111111</option>
<option value=2>2222222</option>
<option value=3>3333333</option>
<option value=4>4444444</option>
<option value=5>5555555</option>
<option value=6>6666666</option>
<option value=7>7777777</option>
</select>
</td>
<td>
<input type="button" value="增 加>>>" onclick="add()"><br><br>
<input type="button" value="<<<删 除" onclick="del()">
</td>
<td width=160>
<select size=10 name="list2" style="width:120">
</select>
</td>
</tr>
</table>
function add_singer(object,value,text)//添加数据
{
if(singer_exist(object,value)==false)
{
object.options.add(new Option(text,value,true,true));
return true;
}
return false;
}
function remove_singer(object,index)//删除数据
{
if(index<0) return false;
object.options.remove(index)
}
function singer_exist(object,value)//检查是否存在
{
for(var i=0;i<object.options.length; i++)
{
if(object.options[i].value==value)
return true;
}
return false;
}
function add()
{
var obj1=document.all.list1;
var index=obj1.selectedIndex;
if (index<0) return false;
value=obj1.options[index].value;
text=obj1.options[index].text;
var obj2=document.all.list2;
add_singer(obj2,value,text)
remove_singer(obj1,index)
}function del()
{
var obj2=document.all.list2;
var index=obj2.selectedIndex;
if (index<0) return false;
value=obj2.options[index].value;
text=obj2.options[index].text;
var obj1=document.all.list1;
add_singer(obj1,value,text)
remove_singer(obj2,index)
}
</script><table>
<tr align=center>
<td width=160>
<select size=10 name="list1" style="width:120">
<option value=1>1111111</option>
<option value=2>2222222</option>
<option value=3>3333333</option>
<option value=4>4444444</option>
<option value=5>5555555</option>
<option value=6>6666666</option>
<option value=7>7777777</option>
</select>
</td>
<td>
<input type="button" value="增 加>>>" onclick="add()"><br><br>
<input type="button" value="<<<删 除" onclick="del()">
</td>
<td width=160>
<select size=10 name="list2" style="width:120">
</select>
</td>
</tr>
</table>
<%
If Request.ServerVariables("request_method")="POST" then
dim lists
lists=split(request.form("list2"),",")
for i=0 to ubound(lists)
conn.execute "insert into seleteduser(UserID) values("& trim(lists(i)) &")"
END If
%>
<script language="javascript">
function add_singer(object,value,text)//添加数据
{
if(singer_exist(object,value)==false)
{
object.options.add(new Option(text,value,true,true));
return true;
}
return false;
}
function remove_singer(object,index)//删除数据
{
if(index<0) return false;
object.options.remove(index)
}
function singer_exist(object,value)//检查是否存在
{
for(var i=0;i<object.options.length; i++)
{
if(object.options[i].value==value)
return true;
}
return false;
}
function add(num)
{
var obj1=document.all.list1;
var obj2=document.all.list2;
len=obj1.options.length
for(i=len-1;i>=0;i--)
{
if(obj1.options[i].selected ==false && num==0)
continue;
value=obj1.options[i].value;
text=obj1.options[i].text;
add_singer(obj2,value,text)
remove_singer(obj1,i)
}
}function del(num)
{
var obj1=document.all.list1;
var obj2=document.all.list2;
len=obj2.options.length
for(i=len-1;i>=0;i--)
{
if(obj2.options[i].selected ==false && num==0)
continue;
value=obj2.options[i].value;
text=obj2.options[i].text;
add_singer(obj1,value,text)
remove_singer(obj2,i)
}
}
function check()
{
var obj=document.all.list2;
if(obj.options.length==0)
{
alert("没有数据");
return false;
}
for(i=0;i<obj.options.length;i++)
obj.options[i].selected=true;
return true;
}</script>
<form name="form1" method=post onsubmit="return check()" action="test.asp">
<table>
<tr align=center>
<td width=160>
<select size=10 name="list1" style="width:120" multiple>
<%
set rs=conn.execute("select * from [user]")
do while not rs.eof
%>
<option value=<%=rs("UserID")%>><%=rs("name")%></option>
<%
rs.movenext
loop
rs.close
set rs=nothing
%>
</select>
</td>
<td>
<input type="button" value="增 加>>>" onclick="add(0)"><br>
<input type="button" value="全添加>>>" onclick="add(1)"><br>
<input type="button" value="<<<删 除" onclick="del(0)"><br>
<input type="button" value="<<<全删除" onclick="del(1)">
</td>
<td width=160>
<select size=10 name="list2" style="width:120" multiple>
</select>
</td>
</tr>
</table>
<input type="submit">
</form>
谢谢你的代码,但是有一个问题
我用的是PHP,看了一下你的ASP接收提交变量的代码
<%
If Request.ServerVariables("request_method")="POST" then
dim lists
lists=split(request.form("list2"),",")
for i=0 to ubound(lists)
conn.execute "insert into seleteduser(UserID) values("& trim(lists(i)) &")"
END If
%>在PHP中,如果想接受来自multiple的列表框变量的话,
需要将
<select size=10 name="list2" style="width:120" multiple>
改为
<select size=10 name="list2[]" style="width:120" multiple>
然后利用
$list2=$_POST['list2'];
$num=count($list2);
for ($i=0,$i<$num,$i++){
echo $list[$i];
}
来接收多重变量,可是这样一来,因为name="list2"变成了name="list2[]" ,所以javascript代码就失去作用了,请问如何解决这个问题呢?
var obj1=document.getElementById("list1[]");
没有ID
var obj1=document.form1.elements["list1[]"];
或
var obj1=document.getElementsByName("list1[]")[0];