Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用select2多选填充选定值_Javascript_Jquery_Onload_Select2 - Fatal编程技术网

Javascript 使用select2多选填充选定值

Javascript 使用select2多选填充选定值,javascript,jquery,onload,select2,Javascript,Jquery,Onload,Select2,我有一个select2从下拉列表中选择多个选项,我也有它选择多个选项。是否可以通过以某种方式更改输入字段的值来使用在页面加载时选择的值填充输入字段,以便在加载页面时已经选择了选项 我的部分代码如下: $('#fruitSelect').select2({ multiple: true, placeholder: "Select fruits...", data: FRUIT_GROUPS, quer

我有一个select2从下拉列表中选择多个选项,我也有它选择多个选项。是否可以通过以某种方式更改输入字段的值来使用在页面加载时选择的值填充输入字段,以便在加载页面时已经选择了选项

我的部分代码如下:

 $('#fruitSelect').select2({
            multiple: true,
            placeholder: "Select fruits...",
            data: FRUIT_GROUPS,
            query: function (options) {
                var selectedIds = options.element.select2('val');
                var data = jQuery.extend(true, {}, FRUIT_GROUPS);
                var selectableGroups = $.map(data, function (group) {
                    var areAllChildrenSelected = true,
                        parentMatchTerm = false,
                        anyChildMatchTerm = false;
                    if (group.text.toLowerCase().indexOf(options.term.toLowerCase()) >= 0) {
                        parentMatchTerm = true;
                    }
                    var i = group.children.length
                    while (i--) {
                        var child = group.children[i];

                        if (selectedIds.indexOf(child.id) < 0) {
                            areAllChildrenSelected = false;
                        };

                        if (options.term == '' || (child.text.toLowerCase().indexOf(options.term.toLowerCase()) >= 0)) {
                            anyChildMatchTerm = true;
                        }
                        else if (!parentMatchTerm) {
                            var index = group.children.indexOf(child);
                            if (index > -1) {
                                group.children.splice(index, 1);
                            };
                        };
                    };

                    return (!areAllChildrenSelected && (parentMatchTerm || anyChildMatchTerm)) ? group : null;
                });

                options.callback({ results: selectableGroups });
            }
        }).on('select2-selecting', function (e) {
            var $select = $(this);
            if (e.val == '') {
                e.preventDefault();
                $select.select2('data', $select.select2('data').concat(e.choice.children));
                $select.select2('close');
            }
        });