7

我正在使用 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();

        });
    });
4

1 回答 1

6

你的脚本有错误。我有同样的问题,我看到了你的问题。在阅读了 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
                };
            },
        },
    });
于 2014-08-15T06:50:23.290 回答