列表选项左右互相移动

第一种写法

<html>
<head>
    <title>Select Op</title>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
    <script type="text/javascript">
        var o;
        function MoveTo(index)
        {
            if (index!=undefined)
            {
                o=source.options[index];
                distinct.options.add(new Option(o.text,o.value));
                source.remove(index);
            }
            else
            {
                for(var i=0;i<source.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(source.options[i].selected)
                    {
                        o=source.options[i];
                        distinct.options.add(new Option(o.text,o.value));
                        source.options.remove(i);
                        i=0;
                    }
                    else
                        i++;
                }
            }
        }
        function MoveBack(index)
        {
            if (index!=undefined)
            {
                o=distinct.options[index];
                source.options.add(new Option(o.text,o.value));
                distinct.remove(index);
            }
            else
            {
                for(var i=0;i<distinct.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(distinct.options[i].selected)
                    {
                        o=distinct.options[i];
                        source.options.add(new Option(o.text,o.value));
                        distinct.options.remove(i);
                        i=0;
                    }
                    else
                        i++;
                }
            }
        }
    </script>
</head>
<body>
<table width='500' border='1' cellpadding='0' cellspacing='0'>
    <tr>
        <td width='45%' align="center">
            <select id='source' multiple="multiple" style="width:200px; height: 300px;" ondblclick="MoveTo(this.selectedIndex)">
            <option value='1'>Item1</option>
            <option value='2'>Item2</option>
            <option value='3'>Item3</option>
            <option value='4'>Item4</option>
            <option value='5'>Item5</option>
            <option value='6'>Item6</option>
            </select>
        </td>
        <td width='10%' align="center"><input type="button" value=">>" onclick="MoveTo()" /><br /><input type="button" value="<<" onclick="MoveBack()" /></td>
        <td width='45%'><select id='distinct' ondblclick="MoveBack(this.selectedIndex)" style="width:200px; height:300px" multiple="multiple" align="center"></select></td>
    </tr>
</table>
</body>
</html>

第二种写法

<html>
<head>
    <title>Select Op</title>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<table width='500' border='1' cellpadding='0' cellspacing='0'>
    <tr>
        <td width='45%' align="center"><select id='source' multiple="multiple" style="width:200; height:300" ondblclick="MoveTo(this.selectedIndex)">
            <option value='1'>Item1</option>
            <option value='2'>Item2</option>
            <option value='3'>Item3</option>
            <option value='4'>Item4</option>
            <option value='5'>Item5</option>
            <option value='6'>Item6</option>
        </select></td>
        <td width='10%' align="center"><input type="button" id="button1" value=">>"  /><br /><input type="button" id="button2" value="<<"  /></td>
        <td width='45%'><select id='distinct' ondblclick="MoveBack(this.selectedIndex)" style="width:200; height:300" multiple="multiple" align="center"></select></td>
    </tr>
</table>
</body>
<script type="text/javascript">
	$(document).ready(function(){
		var o;
        MoveTo = function(index)
        {
            if (index!=undefined)
            {
                o=source.options[index];
                distinct.options.add(new Option(o.text,o.value));
                source.remove(index);
            }
        }

        MoveBack = function (index)
        {
            if (index!=undefined)
            {
                o=distinct.options[index];
                source.options.add(new Option(o.text,o.value));
                distinct.remove(index);
            }
        }
	
		$("#button1").click(function(){
			for(var i=0;i<source.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(source.options[i].selected)
                    {
                        o=source.options[i];
                        distinct.options.add(new Option(o.text,o.value));
                        source.options.remove(i);
                        i=0;
                    }
                    else{
                        i++;
						}
                }
		});
		$("#button2").click(function(){
			for(var i=0;i<distinct.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(distinct.options[i].selected)
                    {
                        o=distinct.options[i];
                        source.options.add(new Option(o.text,o.value));
                        distinct.options.remove(i);
                        i=0;
                    }
                    else{
                        i++;
						}
                }
		});
	});
    </script>
</html>

第三版

<html>
<head>
    <title>Select Op</title>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
	<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<table width='500' border='1' cellpadding='0' cellspacing='0'>
    <tr>
        <td width='45%' align="center"><select id='source' multiple="multiple" style="width:200; height:300" ondblclick="MoveTo(this.selectedIndex)">
            <option value='1'>Item1</option>
            <option value='2'>Item2</option>
            <option value='3'>Item3</option>
            <option value='4'>Item4</option>
            <option value='5'>Item5</option>
            <option value='6'>Item6</option>
        </select></td>
        <td width='10%' align="center"><input type="button" id="button1" value=">>"  /><br /><input type="button" id="button2" value="<<"  /></td>
        <td width='45%'><select id='distinct' ondblclick="MoveBack(this.selectedIndex)" style="width:200; height:300" multiple="multiple" align="center"></select></td>
		<br>
    </tr>
</table>
</body>
<script type="text/javascript">
	$(document).ready(function(){
		var o;
        MoveTo = function(index)
        {
			//将jquery对象转化为dom元素
			var   mySelect = $("#source").get(0);
			var   yourSelect = $("#distinct").get(0);
            if (index!=undefined)
            {
                o=mySelect.options[index];
                yourSelect.options.add(new Option(o.text,o.value));
                mySelect.remove(index);
            }
        }

        MoveBack = function (index)
        {	
			//将jquery对象转化为dom元素
			var   mySelect = $("#source").get(0);
			var   yourSelect = $("#distinct").get(0);
            if (index!=undefined)
            {
                o=yourSelect.options[index];
                mySelect.options.add(new Option(o.text,o.value));
                yourSelect.remove(index);
            }
        }
	
		$("#button1").click(function(){
			//将jquery对象转化为dom元素
			var   mySelect = $("#source").get(0);
			var   yourSelect = $("#distinct").get(0);
			for(var i=0;i<mySelect.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(mySelect.options[i].selected)
                    {
                        o=mySelect.options[i];
                        yourSelect.options.add(new Option(o.text,o.value));
                        mySelect.options.remove(i);
                        i=0;
                    }
                    else{
                        i++;
						}
                }
		});
		$("#button2").click(function(){
			//将jquery对象转化为dom元素
			var   mySelect = $("#source").get(0);
			var   yourSelect = $("#distinct").get(0);
			for(var i=0;i<yourSelect.options.length;)//不要在这里控制i的增长,因为有可能删除了option
                {
                    if(yourSelect.options[i].selected)
                    {
                        o=yourSelect.options[i];
                        mySelect.options.add(new Option(o.text,o.value));
                        yourSelect.options.remove(i);
                        i=0;
                    }
                    else{
                        i++;
						}
                }
		});
	});
    </script>
</html>

第四版

<html>
<head>
    <title>Select Op</title>
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
    <script src="WEB-INF/js/jquery-1.11.3.min.js"></script>
</head>
<body>
<table width='500' border='1' cellpadding='0' cellspacing='0'>
    <tr>
        <td width='45%' align="center"><select id='source' multiple="multiple" style="width: 200px; height: 300px;" ondblclick="MoveTo(this.selectedIndex)">
            <option value='1'>Item1</option>
            <option value='2'>Item2</option>
            <option value='3'>Item3</option>
            <option value='4'>Item4</option>
            <option value='5'>Item5</option>
            <option value='6'>Item6</option>
        </select></td>
        <td width='10%' align="center">
            <input type="button" id="button1" value=">>"  />
            <br /><input type="button" id="button2" value="<<"  />
            <br /><input type="button" id="button3" value="获取全部值" />
        </td>
        <td width='45%'><select id='distinct' ondblclick="MoveBack(this.selectedIndex)" style="width:200px; height:300px" multiple="multiple" align="center"></select></td>
        <br>
    </tr>
</table>
</body>
<script type="text/javascript">
    $(document).ready(function(){
        var o;
        debugger;
        getAll();
        MoveTo = function(index)
        {
            //将jquery对象转化为dom元素
            var   mySelect = $("#source").get(0);
            var   yourSelect = $("#distinct").get(0);
            if (index!=undefined)
            {
                o=mySelect.options[index];
                yourSelect.options.add(new Option(o.text,o.value));
                mySelect.remove(index);
            }
        }

        MoveBack = function (index)
        {
            //将jquery对象转化为dom元素
            var   mySelect = $("#source").get(0);
            var   yourSelect = $("#distinct").get(0);
            if (index!=undefined)
            {
                o=yourSelect.options[index];
                mySelect.options.add(new Option(o.text,o.value));
                yourSelect.remove(index);
            }
        }

        $("#button1").click(function(){
            //将jquery对象转化为dom元素
            var   mySelect = $("#source").get(0);
            var   yourSelect = $("#distinct").get(0);
            for(var i=0;i<mySelect.options.length;)//不要在这里控制i的增长,因为有可能删除了option
            {
                if(mySelect.options[i].selected)
                {
                    o=mySelect.options[i];
                    yourSelect.options.add(new Option(o.text,o.value));
                    mySelect.options.remove(i);
                    i=0;
                }
                else{
                    i++;
                }
            }
        });
        $("#button2").click(function(){
            //将jquery对象转化为dom元素
            var   mySelect = $("#source").get(0);
            var   yourSelect = $("#distinct").get(0);
            for(var i=0;i<yourSelect.options.length;)//不要在这里控制i的增长,因为有可能删除了option
            {
                if(yourSelect.options[i].selected)
                {
                    o=yourSelect.options[i];
                    mySelect.options.add(new Option(o.text,o.value));
                    yourSelect.options.remove(i);
                    i=0;
                }
                else{
                    i++;
                }
            }
        });
    });
    function getAll(){
        var array = new Array();  //定义数组
        //获取右边框中的全部值并转换为数组
        $("#button3").on("click",function (){
            $("#distinct option").each(function () {
                var txt=$(this).val();
                if (txt!=''){
                    array.push(txt);
                }
            });
            if (array.length>0){
                console.log(array);
            }
            else{
                console.log("右边没有选项,请先选择值")
            }
        });
    }
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值