简体   繁体   English

如果 initSelection() 未在 select2 插件中定义错误,则无法调用 val()

[英]cannot call val() if initSelection() is not defined error in select2 plugin

I am using select2 plugin to load remote data.我正在使用 select2 插件加载远程数据。 I am using an aspx page which returns JSON data and same is assigned to select2 plugin.我正在使用返回 JSON 数据的 aspx 页面,并将相同的数据分配给 select2 插件。 After user selects some value from the select2 textbox, i am forcing page to postback.用户从 select2 文本框中选择一些值后,我强制页面回发。 After the postback i am using following code to reload to set the text in the select2 textbox.回发后,我使用以下代码重新加载以设置 select2 文本框中的文本。

var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };

            $('#e6').select2('val', '123');

But system is throwing following error: cannot call val() if initSelection() is not defined但是系统抛出以下错误: cannot call val() if initSelection() is not defined

Even if I define init, I am not able to set the value.即使我定义了 init,我也无法设置该值。 I am using following code.我正在使用以下代码。 Please help me set the value on the select2 textbox after the postback.请帮我在回发后设置 select2 文本框上的值。

$(document).ready(function () {
        $("#e6").select2({
            placeholder: "Search for a movie",
            minimumInputLength: 1,
            ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                url: "data.aspx", 
                dataType: 'json',
                quietMillis: 1000,
                data: function (term, page) {
                    return {
                        name: term
                    };
                },
                initSelection: function (element, callback) {
                    var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
                    callback(data);
                },

                results: function (data) {
                    var results = [];
                    $.each(data, function (index, item) {
                        results.push({
                            id: item['Email'],
                            text: item['PatientID']
                        });
                    });
                    return {
                        results: results
                    };
                },
            },
        });

    });

    window.onload = function () {
        var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
        //When this is called system is throwing error
        //This code is required to show the value in select2 textbox after the post back
        $('#e6').select2('val', data);
    }

    $(document).ready(function () {
        $("#e6").on("select2-selecting", function (e) {
            //alert("selecting val=" + e.val + " choice=" + JSON.stringify(e.choice));
            var id = document.getElementById('<%= savebtn.ClientID %>');
            document.getElementById('<%= hdnFld.ClientID %>').value = e.val;
            id.value = e.val;
            //causes post back
            id.click();

        });
    });

There is a mistake with your script.你的脚本有错误。 I had the same problem and I saw your question.我有同样的问题,我看到了你的问题。 After I read the API docs of Select2 I realised my error.在阅读 Select2 的 API 文档后,我意识到了我的错误。

You should place the initSelection at the same level as ajax .您应该将initSelection放在与ajax相同的级别。 eg例如

$("#e6").select2({
        placeholder: "Search for a movie",
        minimumInputLength: 1,
        initSelection: function (element, callback) {
                var data = { "PatientID": "XYX", "Email": "testing@gmail.com" };
                callback(data);
            },
        ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
            url: "data.aspx", 
            dataType: 'json',
            quietMillis: 1000,
            data: function (term, page) {
                return {
                    name: term
                };
            },


            results: function (data) {
                var results = [];
                $.each(data, function (index, item) {
                    results.push({
                        id: item['Email'],
                        text: item['PatientID']
                    });
                });
                return {
                    results: results
                };
            },
        },
    });

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

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