简体   繁体   English

使用jQuery在相同位置将列表框项移动和删除到另一个列表框

[英]Move and remove Listbox item to another listbox using jQuery in same places

I have 2 multi select boxes like as in this link. 我有2个多重选择框,如此链接中所示。 http://jsfiddle.net/bdMAF/38/ http://jsfiddle.net/bdMAF/38/

$(function(){
    $("#button1").click(function(){
        $("#list1 > option:selected").each(function(){
            $(this).remove().appendTo("#list2");
        });
    });

    $("#button2").click(function(){
        $("#list2 > option:selected").each(function(){
            $(this).remove().appendTo("#list1");
        });
    });
});

But When i add from one first select box to second select box it is working fine for me.But again when i add from second select box to first select box they are appending to last of first select box.But what i want is i need to add they must be added in the place where they deleted. 但是当我从一个第一个选择框添加到第二个选择框时,它对我来说很好。但是再次,当我从第二个选择框添加到第一个选择框时,它们附加到第一个选择框的最后一个。但是我想要的是我需要的要添加,必须将它们添加到删除的位置。

Thanks in advance... 提前致谢...

Maybe you can simply set and remove the attribute "disabled" but it will leave a blank space in the options. 也许您可以简单地设置和删除属性“ disabled”,但是它将在选项中保留空白。 Note that with this method you will need to clone the option the first time. 请注意,使用这种方法,您将需要在第一时间克隆该选项。

The other solution whould be to add the content as usual but applying a sort() function before 另一个解决方案是照常添加内容,但在此之前应用sort()函数

function byValue(a, b) {
    return a.value > b.value ? 1 : -1;
};

function rearrangeList(list) {
    $(list).find("option").sort(byValue).appendTo(list);
}

$(function () {
    $("#button1").click(function () {
        $("#list1 > option:selected").each(function () {
            $(this).remove().appendTo("#list2");
            rearrangeList("#list2");
        });
    });

    $("#button2").click(function () {
        $("#list2 > option:selected").each(function () {
            $(this).remove().appendTo("#list1");
            rearrangeList("#list1");
        });
    });
});

You can try at this fiddle 你可以试试这个小提琴

You need to keep track of some sort of index, I think in your case you can use the value of each option. 您需要跟踪某种索引,我认为您可以使用每个选项的value (but you could use a dedicated data-* attribute if you need to) (但如果需要,您可以使用专用的data-*属性)

With that value you can then search the current list and see where it should fit in. Loop the options and check for a value greater than the one you are moving. 使用该值,您可以搜索当前列表并查看其适合的位置。循环选择选项,并检查是否有一个大于要移动的值的值。 If you find one then insert it before that, if you don't fine one then insert at the end. 如果找到一个,则在此之前插入它;如果没有找到一个,则在最后插入。

This should do it: 应该这样做:

$("#button2").click(function(){
    $("#list2 > option:selected").each(function(){
        var item = $(this).remove();
        var match = null;
        $("#list1").find("option").each(function(){
            if($(this).attr("value") > item.attr("value")){
                match = $(this);
                return false;
            }
        });
        if(match)
            item.insertBefore(match);
        else
           $("#list1").append(item);
    });
});

You can apply the same for the reverse too. 您也可以将其应用于相反的方向。

Here is a working example 这是一个有效的例子

After adding the options back to #list1 , a simple sort() will do the rest. 将选项添加回#list1 ,其余的操作将由一个简单的sort()完成。 For that we need to add a comparison function to it based on its value . 为此,我们需要根据其向其添加比较函数。

$(function () {
    $("#button1").click(function () {
        $("#list1 > option:selected").each(function () {
            $(this).remove().appendTo("#list2");
        });
    });

    $("#button2").click(function () {
        $("#list2 > option:selected").each(function () {
            $(this).remove().appendTo("#list1");
            var opts = $('#list1 option').get();

            $('#list1 ').html(opts.sort(optionSort));
        });
    });

    function optionSort(a, b) {
        return $(a).val() > $(b).val();
    }
});

Check out this JSFiddle 看看这个JSFiddle

You can also sort using text() instead of val() by changing it in the optionSort() . 您还可以通过在optionSort()进行更改来使用text()而不是val()进行optionSort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM