jQuery Select2 和 initSelection 回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18904132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:44:23  来源:igfitidea点击:

Select2 and initSelection callback

jqueryjquery-select2

提问by Mad Marvin

On page load, I'm trying to use initSelection to select ID 60 (specified value of the input field). I can't seem to get it to work properly.

在页面加载时,我尝试使用 initSelection 来选择 ID 60 (specified value of the input field)。我似乎无法让它正常工作。

The PHP scripts work great and return the correct values, but how do I get the JS to do the callback correctly?

PHP 脚本运行良好并返回正确的值,但如何让 JS 正确执行回调?

JavaScript:

JavaScript:

$(document).ready(function() {
    $('#editAlbumArtistId').select2({
            placeholder: 'Search names',
            ajax: {
                url: "/jQueryScripts/jQuerySelectListArtists.php",
                dataType: 'json',
                quietMillis: 100,
                data: function (term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10 // page size
                    };
                },
                results: function (data, page) {
                    return {results: data.results};
                }

            },
            initSelection: function(element, callback) {

            var id = $(element).val();
            if(id !== "") {
                $.ajax("/jQueryScripts/jQuerySelectListArtists.php", {
                    data: {id: id},
                    dataType: "json"
                }).done(function(data) {
                    callback(data);
                });
            }
        }
    });
});

HTML:

HTML:

<p>
    <input type='hidden' value="60" data-init-text='Search names' name='editAlbumArtistId' id='editAlbumArtistId' style="width:180px;"/>
</p>

Every time I refresh the page, I see that the PHP script gets executed and that it returns the proper ID and text. However, the field isn't updated and I've seriously tried everything I can think of.

每次刷新页面时,我都会看到 PHP 脚本被执行并返回正确的 ID 和文本。但是,该领域没有更新,我已经认真尝试了我能想到的一切。

I'm using Select2 3.4.3.

我正在使用 Select2 3.4.3。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Mad Marvin

Finally solved it! I figured select2didn't want an array since it's a single value, so I selected the first element of the data.results array.

终于解决了!我想select2不需要数组,因为它是单个值,所以我选择了 data.results 数组的第一个元素。

callback(data.results[0]);

And if you have set multiple:true, just accept the entire results array;

如果你设置了 multiple:true,就接受整个结果数组;

callback(data.results);

回答by Juan Jose

You could also use this for less code:

您也可以使用它来减少代码:

    initSelection: function(element, callback) {
        return $.getJSON("/jQueryScripts/jQuerySelectListArtists.php?id=" + element.val(), null, function(data) {
            return callback(data[0]);
        });
    }

I saw this example in the Select2 wiki, but i had to deal with callback(data) vs callback(data[0]) like you did.

我在 Select2 wiki 中看到了这个例子,但我不得不像你一样处理 callback(data) 和 callback(data[0])。