jQuery Select2:将选定的选项放到其他容器中

时间:2013-09-16 10:15:02

标签: jquery jquery-select2

有没有办法将选定的选项放到其他容器中?

与附图相似:

enter image description here

1 个答案:

答案 0 :(得分:3)

检查JSFiddle (updated)。当您从#source中选择一个选项时,它会移至#target(在此示例中,未移动默认值为零的值):

$(document).on('change', '#source', function() {
    $option = $(this).find('option:selected');
    if ($option.val()) {
        $option.detach();
        $option.appendTo('#target');

        //select2 adjustments
        $('#source').select2('val', '0');
        $('#target').select2();
    }
});