javascript - 在 select2 jquery 插件中没有调用 initSelection

标签 javascript jquery jquery-select2

我正在使用来自 http://ivaynberg.github.io/select2/ 的 select2 jquery 插件.

我正在使用以下代码。

 $(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: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
                    dataType: 'jsonp',
                    quietMillis: 1000,
                    data: function (term, page) {
                        return {
                            q: term, // search term
                            page_limit: 10,
                            apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
                        };
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter remote JSON data
                        return { results: data.movies };
                    }
                },
                initSelection: function (element, callback) {
                    var id = 9942;//$(element).val();
                    alert('initSelection');
                    if (id !== "") {
                        $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
                            data: {
                                apikey: "ju6z9mjyajq2djue3gbvv26t"
                            },
                            dataType: "jsonp"
                        }).done(function (data) { callback(data); });
                    }
                },

                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
                escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
            });

        });


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

            });
        });

问题:由于某些原因,没有调用 initSelection,因此,我无法跨回发设置文本框的值。

我正在使用从 http://ivaynberg.github.io/select2/ 加载远程数据的示例网站。

我查看了 documentation对于 initSelection,它说“只有在有要处理的初始输入时才会调用此函数。”,我不确定它到底是什么意思。

我做错了什么吗?请帮忙

最佳答案

您应该提供初始值:

<input ... value="192" ... />

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

对于多个值,要么:

<input ... value="192,193" ... />

$('#e6').select2('val', [192,193]);

参见 JSFiddle .

关于javascript - 在 select2 jquery 插件中没有调用 initSelection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252428/

相关文章:

javascript - 创建带有文本选择 anchor 的 URL

jquery 选择的克隆不工作

jquery - 使用 select2 自动标记粘贴字符串中的最后一项

javascript - jquery select2 - 通过 AJAX php 格式化结果

jquery - 我怎样才能使用jquery-ui的toggleClass更好地动画化它?

jquery - 替换 select2 选项?

javascript - 删除数组对象 es6 中的属性

java - 通过深入研究 js 源代码来解决 HTML 的问题

javascript - 为什么我的组件在传递 props( react )时没有渲染?

javascript - 使用 jquery 和 php 的 ajax