| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖
标题:右边选中的项目没有全部被删除?函数要怎么改进
只看楼主 加入收藏
jin522
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-10-11
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:3 
右边选中的项目没有全部被删除?函数要怎么改进
程序代码:
	
		
			
图片附件: 游客没有浏览图片的权限,请 登录注册
<html> <head> <script language="javascript"> function selectChange() { var allNumber=document.selectForm.allItem.options.length; for(var i=0;i<allNumber;i++) { document.selectForm.allItem.options[0]=null; } if(document.selectForm.profession.options[document.selectForm.profession.selectedIndex].text=="学生") { document.selectForm.allItem.options[0]=new Option("数学","数学"); document.selectForm.allItem.options[1]=new Option("语文","语文"); document.selectForm.allItem.options[2]=new Option("英语","英语"); document.selectForm.allItem.options[3]=new Option("Java基础","Java基础"); document.selectForm.allItem.options[2]=new Option("JavaScript应用","JavaScript应用"); } else if(document.selectForm.profession.options[document.selectForm.profession.selectedIndex].text=="运动员") { document.selectForm.allItem.options[0]=new Option("足球","足球"); document.selectForm.allItem.options[1]=new Option("篮球","篮球"); document.selectForm.allItem.options[2]=new Option("排球","排球"); document.selectForm.allItem.options[3]=new Option("网球","网球"); document.selectForm.allItem.options[4]=new Option("羽毛球","羽毛球"); document.selectForm.allItem.options[5]=new Option("游泳","游泳"); document.selectForm.allItem.options[6]=new Option("武术","武术"); document.selectForm.allItem.options[7]=new Option("登山","登山"); } } function checkData(selectedItem)/*检测项目是否已被添加*/ { var flag=true; for(var i=0;i<document.selectForm.selectedItem.options.length;i++) { if(document.selectForm.selectedItem.options[i].text==selectedItem.text) { flag=false; break; } } return flag; } function addSelectedItem()/*添加选中*/ { for(var i=0;i<document.selectForm.allItem.options.length;i++) { if(document.selectForm.allItem.options[i].selected) { if(checkData(document.selectForm.allItem.options[i])) { document.selectForm.selectedItem.options[document.selectForm.selectedItem.length]=new Option (document.selectForm.allItem.options[i].text,document.selectForm.allItem.options[i].value); } } } } function QaddAll()/*添加全部*/ { for(var i=0;i<document.selectForm.selectedItem.length;i++) { document.selectForm.selectedItem.options[i]=null; } for(var i=0;i<document.selectForm.allItem.options.length;i++) { document.selectForm.selectedItem.options[i]=new Option(document.selectForm.allItem.options [i].text,document.selectForm.allItem.options[i].value); } } function deleteSelectedItem()/*删除选中*/ { for(var i=0;i<document.selectForm.selectedItem.length;i++) { if(document.selectForm.selectedItem.options[i].selected) { document.selectForm.selectedItem.options[i]=null; } } } function deleteAll()/*删除全部*/ { var number=document.selectForm.selectedItem.options.length; for(var i=0;i<number;i++) { document.selectForm.selectedItem.options[0]=null; } } </script> </head> <body> <form name="selectForm"> <h5><font size="3">Example:select对象的应用</font></h5> <hr> <b><font size="3">&nbsp;选择用户类别:</font></b> <select size="1" name="profession" style="border-style:solid; width:150; height:1" onChange="selectChange()"> <option value="学生" selected>学生</option> <option value="运动员">运动员</option> </select> <br><br> <b><font size="3">&nbsp;请选择您曾经学过的课程或者参与过的项目:</font></b> <table border="0" width="560" id="table1" height="210"> <tr> <td width="180" rowspan="6" height="204" align="center"> <select size="13" name="allItem" multiple style="width:150;"> <option value="数学">数学</option> <option value="语文">语文</option> <option value="英语">英语</option> <option value="Java基础">Java基础</option> <option value="JavaScript应用">JavaScript应用</option> </select> </td> <td width="200" height="30"> </td> <td width="180" rowspan="6" height="204" align="center"> <select size="13" name="selectedItem" multiple style="width:150;"> </select> </td> </tr> <tr> <td width="200" height="30" align="center"> <input type="button" value="添加选中" name="addSelect" style="width:100; height=:25" onClick="addSelectedItem()"> </td> </tr> <tr> <td width="200" height="30" align="center"> <input type="button" value="添加全部" name="addAll" style="width:100; height=:25" onClick="QaddAll()"> </td> </tr> <tr> <td width="200" height="30" align="center"> <input type="button" value="删除选中" name="delectSelect" style="width:100; height=:25" onClick="deleteSelectedItem()"> </td> </tr> <tr> <td width="200" height="30" align="center"> <input type="button" value="删除全部" name="addAll" style="width:100; height=:25" onClick="deleteAll()"> </td> </tr> <tr> <td width="145" height="37" align="center"> </td> </tr> </table> </form> </body> </html>
为什么“删除选中”按钮不尽人意,我选了几个,点“删除选中”,但选中的没有全部被删除?为啥,请指导下函数哪里需要改进。
搜索更多相关主题的帖子: 函数 项目 改进 删除 
2010-12-13 13:26
jin522
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-10-11
收藏
得分:0 
为什么“删除选中”按钮不尽人意,我选了几个,点“删除选中”,但选中的没有全部被删除?为啥,请指导下函数哪里需要改进。
2010-12-13 13:27
sclorg
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:55
专家分:152
注 册:2010-4-16
收藏
得分:20 
for (var i=document.selectForm.selectedItem.options.length-1; i>=0; i--)
    {
        if (document.selectForm.selectedItem.options[i].selected)
        {
            document.selectForm.selectedItem.options[i].remove(i);
        }
    }
2010-12-15 13:44
jin522
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-10-11
收藏
得分:0 
多谢
2010-12-18 13:00
快速回复:右边选中的项目没有全部被删除?函数要怎么改进
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved