使用 JQuery 将选定的项目从一个组合框添加到另一个组合框

发布于 2024-12-06 21:51:55 字数 553 浏览 0 评论 0原文

我正在尝试找到将所选项目从一个组合框添加到另一个组合框的最佳方法。诀窍是我只想将尚不存在的项目添加到目标列表中。目前我使用的过程相当丑陋,并且不能按我的预期工作。

$('#addSelectedButton').click(function() {
    var previousOption;
    $('#sourceList option:selected').appendTo('#destinationList');
    $('select[name=destinationList] option').each(function () {
        if (this.text == previousOption) $(this).remove();
        previousOption = this.text;
    });
});

我遇到的问题是 appendTo 方法更多地充当移动而不是添加。然后是删除重复项的问题,这在本例中是有效的,但我忍不住认为有更好的方法。

任何帮助将不胜感激。

谢谢,

I'm trying to find the best way to add selected items from one combobox to another. The trick is I only want to add items to the destination list that don't already exist. Currently the process I use is rather ugly and does not work as I would expect.

$('#addSelectedButton').click(function() {
    var previousOption;
    $('#sourceList option:selected').appendTo('#destinationList');
    $('select[name=destinationList] option').each(function () {
        if (this.text == previousOption) $(this).remove();
        previousOption = this.text;
    });
});

The problem I'm having is that the appendTo method acts as more of a move rather than an add. Then there's the problem of removing the duplicates, which works in this this example, but I can't help but think there's a better way.

Any assistance would be greatly appreciated.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮年 2024-12-13 21:51:55

使用clone()grep()你可以轻松实现这一点。首先克隆从源中选择的选项,然后使用grep可以过滤掉目标列表中已有的项目。

$('#addSelectedButton').click(function() {
    // select this once into a variable to minimize re-selecting
    var $destinationList = $('#destinationList');

    // clone all selected items
    var $items = $.grep($('#sourceList option:selected').clone(), function(v){
        // if the item does not exist return true which includes it in the new array
        return $destinationList.find("option[value='" + $(v).val() + "']").length == 0;

    });

    // append the collection to the destination list
    $destinationList.append($items);
});

工作示例: http://jsfiddle.net/hunter/4GK9A/


clone()

创建匹配元素集的深层副本。

grep()

查找数组中满足过滤函数的元素。原始数组不受影响。

using clone() and grep() you can achieve this easily. First clone the options that are selected from the source and then using grep you can filter out the items that are already in the destination list.

$('#addSelectedButton').click(function() {
    // select this once into a variable to minimize re-selecting
    var $destinationList = $('#destinationList');

    // clone all selected items
    var $items = $.grep($('#sourceList option:selected').clone(), function(v){
        // if the item does not exist return true which includes it in the new array
        return $destinationList.find("option[value='" + $(v).val() + "']").length == 0;

    });

    // append the collection to the destination list
    $destinationList.append($items);
});

Working Example: http://jsfiddle.net/hunter/4GK9A/


clone()

Create a deep copy of the set of matched elements.

grep()

Finds the elements of an array which satisfy a filter function. The original array is not affected.

眼泪淡了忧伤 2024-12-13 21:51:55

我认为您想要的是将“克隆”与附加结合使用:

http://api.jquery.com/克隆/

I think what you want is to use "clone" in conjunction with append:

http://api.jquery.com/clone/

兲鉂ぱ嘚淚 2024-12-13 21:51:55

你可以像这样使用clone():

$('#addSelectedButton').click(function() {
    var previousOption;
    var clone =  $('#sourceList option:selected').clone();
    clone.appendTo('#destinationList');
    $('select[name=destinationList] option').each(function () {
        if (this.text == previousOption) $(this).remove();
        previousOption = this.text;
    });
});

You could use clone() like this:

$('#addSelectedButton').click(function() {
    var previousOption;
    var clone =  $('#sourceList option:selected').clone();
    clone.appendTo('#destinationList');
    $('select[name=destinationList] option').each(function () {
        if (this.text == previousOption) $(this).remove();
        previousOption = this.text;
    });
});
你没皮卡萌 2024-12-13 21:51:55

您只需在目标列表中搜索包含的值即可。 http://jsfiddle.net/EHqem/

$('#addSelectedButton').click(function() {
    $('#sourceList option:selected').each(function(i, el) {
        if ($('#destinationList option[value='+$(el).val()+']').length === 0) {
           $('#destinationList').append($(el).clone());
        }
    });
});

You can just search the destination list for the containing value. http://jsfiddle.net/EHqem/

$('#addSelectedButton').click(function() {
    $('#sourceList option:selected').each(function(i, el) {
        if ($('#destinationList option[value='+$(el).val()+']').length === 0) {
           $('#destinationList').append($(el).clone());
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文