另开一贴,原在asp,关于两个select互排

slysmart 2013-09-13 09:31:16
<%
Dim list
Set conn=Server.CreateObject("ADODB.Connection")
DBPath1=server.mappath("../db/db.mdb")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath1
set rs=conn.execute("select a from b")
do while not rs.eof
list=list&"<option>"&rs("a")&"</option>"
rs.movenext
Loop
Rs.Close:Set Rs=Nothing
Response.write "<select id='scs' onchange='mselect()'>"&list&"</select>"
Response.write "<select id='scscms'>"&list&"</select>"
%>
<script type="text/javascript">
var option="";
function mselect(){
var scs=document.getElementById("scs");
var scscms=document.getElementById("scscms");
var n=scs.selectedIndex;
for(var i =0;i<scscms.options.length; i++){
if(scs.options[n].text==scscms.options[i].text)
scscms.options.remove(i);
}
if(option!="")scscms.add(option);
option=new Option(scs.options[n].text,"");
}
mselect();
</script>

上面mselect()只能做到,选择scs时,scsms中删除,当scsms中选择值时,此时再重新选择scs时,选择项中也要删除scsms选择值
...全文
103 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
slysmart 2013-09-13
  • 打赏
  • 举报
回复
引用 5 楼 slysmart 的回复:
修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
多次来回选择测试,还是有问题,两个select选项里数目不对了
slysmart 2013-09-13
  • 打赏
  • 举报
回复
修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
conanhhy 2013-09-13
  • 打赏
  • 举报
回复
LZ可以参考下面的代码

msselect(document.getElementById("scs"), document.getElementById("scscms"));

var option = "";
function mselect(sourceSelect, targetSelect){
    var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
    
    option = new Option(sourceSelect.options[n].text,"")
}
slysmart 2013-09-13
  • 打赏
  • 举报
回复
引用 2 楼 conanhhy 的回复:
不理解LZ的意思,能否具体说下操作?
两个select,相同的option,在select1选择某项后,此时去看select2中选择项中select1已选择项会删除,同理2中选择后,1中也会删除(注:多次来回选择后某些前面删除项会添加回去的)
conanhhy 2013-09-13
  • 打赏
  • 举报
回复
不理解LZ的意思,能否具体说下操作?
slysmart 2013-09-13
  • 打赏
  • 举报
回复
上面mselect()只能做到,选择scs时,scsms中删除 如何做到当scsms中选择值时,此时再重新选择scs时,选择项中也要删除scsms选择值 也就是互相排斥
slysmart 2013-09-13
  • 打赏
  • 举报
回复
引用 9 楼 conanhhy 的回复:
[quote=引用 8 楼 slysmart 的回复:] [quote=引用 7 楼 conanhhy 的回复:] [quote=引用 6 楼 slysmart 的回复:] [quote=引用 5 楼 slysmart 的回复:] 修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
多次来回选择测试,还是有问题,两个select选项里数目不对了[/quote] 两个select的选项数是不一样的且相差1,因为其中一个下拉列表选择一项后,会移除另一个下拉列表内对应的项。[/quote] 问题是有时相差不止1[/quote] LZ,代码里需要再加判断

var isFind = false;
for(var i =0;i<targetSelect.options.length; i++){
    if(sourceSelect.options[n].text==targetSelect.options[i].text) {
        targetSelect.options.remove(i);
        isFind = true;
    }
}
if (isFind) {
    option = new Option(sourceSelect.options[n].text,"");
} else {
    option = "";
}
[/quote] 谢谢conanhhy的解答
if(option != "") sourceSelect.options.add(option); //if(option != "" || option != "请选择") sourceSelect.options.add(option);改成这里就无效了,报错“类型不匹配”
conanhhy 2013-09-13
  • 打赏
  • 举报
回复
引用 8 楼 slysmart 的回复:
[quote=引用 7 楼 conanhhy 的回复:] [quote=引用 6 楼 slysmart 的回复:] [quote=引用 5 楼 slysmart 的回复:] 修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
多次来回选择测试,还是有问题,两个select选项里数目不对了[/quote] 两个select的选项数是不一样的且相差1,因为其中一个下拉列表选择一项后,会移除另一个下拉列表内对应的项。[/quote] 问题是有时相差不止1[/quote] LZ,代码里需要再加判断

var isFind = false;
for(var i =0;i<targetSelect.options.length; i++){
    if(sourceSelect.options[n].text==targetSelect.options[i].text) {
        targetSelect.options.remove(i);
        isFind = true;
    }
}
if (isFind) {
    option = new Option(sourceSelect.options[n].text,"");
} else {
    option = "";
}
slysmart 2013-09-13
  • 打赏
  • 举报
回复
引用 7 楼 conanhhy 的回复:
[quote=引用 6 楼 slysmart 的回复:] [quote=引用 5 楼 slysmart 的回复:] 修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
多次来回选择测试,还是有问题,两个select选项里数目不对了[/quote] 两个select的选项数是不一样的且相差1,因为其中一个下拉列表选择一项后,会移除另一个下拉列表内对应的项。[/quote] 问题是有时相差不止1
conanhhy 2013-09-13
  • 打赏
  • 举报
回复
引用 6 楼 slysmart 的回复:
[quote=引用 5 楼 slysmart 的回复:] 修改了下
<script type="text/javascript">
var option = "";
function mselect(scs, scsms){
    var scs;
	var scsms;
	var sourceSelect=document.getElementById(scs);
	var targetSelect=document.getElementById(scsms);
	var n=sourceSelect.selectedIndex;
    if(option != "") sourceSelect.options.add(option);
    for(var i =0;i<targetSelect.options.length; i++){
        if(sourceSelect.options[n].text==targetSelect.options[i].text)
            targetSelect.options.remove(i);
    }
     
    option = new Option(sourceSelect.options[n].text,"")
}
</script>
ok了,再来研究多个select,感觉要双循环
多次来回选择测试,还是有问题,两个select选项里数目不对了[/quote] 两个select的选项数是不一样的且相差1,因为其中一个下拉列表选择一项后,会移除另一个下拉列表内对应的项。

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧