简体   繁体   English

Select2不显示initSelection值

[英]Select2 doesn't show the initSelection values

the jquery Select2(Version: 3.5.4) plugin doesn't show my initial value which I loaded on the initSelection function of the plugin. jQuery Select2(版本:3.5.4)插件未显示我在插件的initSelection函数上加载的初始值。 Here is my code: 这是我的代码:

$("#materialFieldTags").select2({
            tags: true,
            initSelection : function (element, callback) {
                console.log(element);
                console.log(callback);
                var countryId = "3"; //Your values that somehow you parsed them
                var countryText = "mater3";
                var data = [];//Array                   
                var tempJSONMat = {
                    materials: []
                };
                $.ajax({
                    url: "php/FormProcessing.php",
                    type: "post",
                    data: "main=" + "materialFault" + "&faultid="+ main.faultId,
                    dataType: 'json',
                    success: function(data){
                        data.forEach(function(column) {
                            //console.log(column);                
                            tempJSONMat.materials.push({ 
                                "id" : column.material_id,
                                "text"  : column.name
                            });                
                        });
                    }
                });
                callback(tempJSONMat.materials[0]);
            },
            ajax: {
                type: "POST",
                url: 'php/FormFilling.php',
                dataType: 'json',
                data: function (params) {                    
                    return "main=" + "allMaterials" + "&searchterm=" + params;
                },
                processResults: function (data, page) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.name,
                                id: item.id
                            };
                        })
                    };
                },
                cache: true
            }
        });

Can you take I look at my code? 可以让我看看我的代码吗? Because I can't see it!! 因为我看不到! I also have try the: 我也尝试过:

$("#materialFieldTags").select2("data",mydata);

After the initialization of the plugin, but I am getting the same result. 在插件初始化之后,但是我得到了相同的结果。

Finally, I found my mistake! 最后,我发现了我的错误! It was define two element by the same id, by mistake! 错误地用相同的id定义了两个元素! The data appeared on first one and I was looking the second one. 数据出现在第一个数据上,而我正在寻找第二个数据。

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

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