再问select表单添加和删除option怎么样,急在线等待

有两个select1,select2表单,select1已有option,通过两个按钮add1,del2
点add1时把select1中选中的项添加到select2中,select1选中项删除
点del2时,del2中选中项删除,返回到select1中的原位置
谢谢!!
---------------------------------------------------------------

送给你一个函数,可以在任意个select间增加或者删除option,只要你传递两个参数过去就行了,
注意:本函数主要用于多选框:

1<select id="Province" multiple="true" name="Province" size="7">
2<option value="1">山东   
3</option></select>
 1<script language="javascript">   
 2/*   
 3*多选下拉列表动态增加函数:Create(SourseObject,TargetObject);   
 4*参数:SourseObject:表示要动态添加的源Select;   
 5*参数:TargetObject:表示要动态接受的目的Select;   
 6*例:Create(document.all.SourSelect,document.all.TarSelect);   
 7*作者:月影飞鸿 于 2003-05-26晚作   
 8*/   
 9function Create(SourceSelect,TargetSelect)   
10{   
11var IsCreate = true;   
12var theIndex = SourceSelect.selectedIndex;   
13var theLength = SourceSelect.length ;   
14if (theIndex == -1 ) //如果源Select为空的话,则退出过程   
15return false;   
16while (IsCreate) //添加到目的Select循环   
17{   
18theValue = SourceSelect.options[theIndex].text; //得到所选择的文本   
19  
20TargetSelect.options.add(new Option(theValue)); //目的Select增加一个文本   
21  
22theIndex = theIndex + 1; //如果是选择多列的话,对下一个进行处理   
23  
24if (theIndex == theLength) //theLength 如果是4的话,则theIndex应该是3,   
25{ //如果两者想等的话,则源Select多了一个值,   
26IsCreate = false; //所以需要退出循环   
27break;   
28}   
29if (SourceSelect.options[theIndex].selected == false)//如果没有被选择的话,则退出循环   
30{   
31IsCreate = false;   
32}   
33}   
34  
35while (IsCreate == false) //删除源select循环   
36{   
37SecIndex = SourceSelect.selectedIndex; //动态得到被选择的索引   
38theLength = SourceSelect.length ; //动态得到Select的长度   
39SourceSelect.remove(SecIndex); //删除指定索引的元素   
40if (theLength == 1) //表示最后一个元素已删除,   
41return false; //源select空了,退出循环   
42if (theLength == SecIndex + 1) //表示多选的已全部删掉,退出循环   
43return false;   
44if (SourceSelect.options[SecIndex].selected == false)   
45{   
46IsCreate = true;   
47}   
48}   
49}   
50</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus