【问题标题】:cannot call val() if initSelection() is not defined error in select2 plugin如果在 select2 插件中未定义 initSelection(),则无法调用 val()
【发布时间】:2013-08-20 16:28:53
【问题描述】:

我正在使用 select2 插件来加载远程数据。我正在使用一个返回 JSON 数据的 aspx 页面,并将其分配给 select2 插件。用户从 select2 文本框中选择一些值后,我强制页面回发。回发后,我使用以下代码重新加载以设置 select2 文本框中的文本。

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

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

但系统抛出以下错误:cannot call val() if initSelection() is not defined

即使我定义了 init,我也无法设置值。我正在使用以下代码。请帮我在回发后在 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();

        });
    });

【问题讨论】:

  • initSelection 不应在 ajax-property 内。

标签: javascript jquery jquery-plugins jquery-select2


【解决方案1】:

您的脚本有误。我有同样的问题,我看到了你的问题。在我阅读了 Select2 的 API 文档后,我意识到了我的错误。

您应该将initSelection 放在与ajax 相同的级别。例如

$("#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
                };
            },
        },
    });

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 2013-08-17
    • 2013-09-25
    • 1970-01-01
    • 2013-04-09
    • 2017-01-04
    • 1970-01-01
    • 2017-06-27
    • 2015-01-13
    相关资源
    最近更新 更多