jQuery将项目从一个选择框移到另一个选择框,同时保留列表顺序 [英] jQuery Move Item from one select box to another while retaining listing order

查看:80
本文介绍了jQuery将项目从一个选择框移到另一个选择框,同时保留列表顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在运行的这段代码,但是当该项目被移动时,它显示在列表的底部.我宁愿按字母顺序排列顺序,也不知道从哪里开始.

I have this code that is working but when the item is moved, it shows up at the bottom of the list. I'd prefer to keep the order alphabetical and have no idea where to start.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

<style type='text/css'>
.whatever {
    width:200px;
    height:50px;
    overflow:auto
}
</style>
<script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){
    $('#select1').click(function(){
    $('#select1 option:selected').appendTo('#select2');
});

$('#select2').click(function(){
    $('#select2 option:selected').appendTo('#select1');
});

$('#select3').click(function(){
    $('#select3 option:selected').appendTo('#select4');
});

$('#select4').click(function(){
    $('#select4 option:selected').appendTo('#select3');
});
});//]]>  
</script>
</head>
<body>
<div>
<select multiple id="select1" class="whatever">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
</select>
<img src="../Common/MultiSelect/img/switch.png">
<select multiple id="select2" class="whatever"></select>
</div>

<div>
<select multiple id="select3" class="whatever">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
</select>
<img src="../Common/MultiSelect/img/switch.png">
<select multiple id="select4" class="whatever"></select>
</div>
</body>
</html>

推荐答案

您可以使用sort方法保持所需的顺序

You can use the sort method to keep the order you want

示例按您的选项值排序

 youroptions.sort(function(a,b){
       return a.value - b.value;
 });

如果您将相对选择元素用作同级div中的同级元素,则可以大大缩短代码

You can shorten your code quite a bit if you have your relative select elements as siblings inside a parent div by using siblings

$(document).ready(function() {
    $('select').change(function() {
        var $this = $(this);
        $this.siblings('select').append($this.find('option:selected')); // append selected option to sibling
        $('select', $this.parent()).each(function(i,v){ // loop through relative selects
            var $options = $(v).find('option'); // get all options
            $options = $options.sort(function(a,b){ // sort by value of options
                return a.value - b.value;
            });
            $(this).html($options); // add new sorted options to select
        });
    });   
});​

http://jsfiddle.net/e6Y7J/

这篇关于jQuery将项目从一个选择框移到另一个选择框,同时保留列表顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆