javascript select2 中的预选值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22481882/
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-10-27 23:15:00  来源:igfitidea点击:

Preselecting value in select2

javascriptjqueryjquery-select2

提问by Smith

Texbox is dynamically filled with a remote call using Select2 and how do I set preselected value. Here is the code

Texbox 使用 Select2 动态填充远程调用以及如何设置预选值。这是代码

<input type="hidden" id="e6">

$("#e6").select2({
    placeholder: "Search for a movie",
    minimumInputLength: 1,
    ajax: {
        url: url,
        dataType: 'jsonp',
        data: function (term, page) {
            return {
                q: term, // search term 
                page_limit: 10, }; 
        }, 
        results: function (data, page) { 
                return {results: data};
        }
    }
});    

I tried this to preselect value 1049

我试过这个来预选值 1049

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

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

but this doesn't set the value on the textbox.

但这不会在文本框上设置值。

Any ideas?

有任何想法吗?

回答by Smith

In case anybody wants to know how to solve this

如果有人想知道如何解决这个问题

$("#e6").select2('data', {id: '1049', text: 'MyLabel'});

回答by VishwaKumar

You can use initSelection method

您可以使用 initSelection 方法

initSelection: function(element, callback) {
callback({id: 1, text: 'default selection with id 1' });
},

Check this linkand loading remote data section here

检查此链接在此处加载远程数据部分

回答by zarget

for multiple selection...

对于多选...

var li = $("#e6");
li.select2({
    placeholder: "Placeholder"
});

var unselected = li.find('option:not(:selected)');
var selected = [];
for (var i = 0; i < unselected.length; i++) {
    selected[i] = { id: unselected[i].value, text: unselected[i].text };
}
li.select2('data', selected);

回答by Alice

The solution that worked for me (and that is decribed in the documentation) was:

对我有用的解决方案(并且在文档中有所描述)是:

$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed

回答by Auggysg

I got it to work with

我得到它的工作

$("#e6").val("input id of the select2 option here").trigger("change")

回答by first day of your life

in 2019 this worked for me

在 2019 年这对我有用

    // Create a DOM Option and pre-select by default~
    var newOption = new Option(data.text, data.id, true, true);
    // Append it to the select
    $('#mySelect2').append(newOption).trigger('change');

回答by Oluebube Princess Egbuna

This worked for me

这对我有用

<script>
  $(function() {
    if (window.formPrefill) {
      // setTimeout to force script to run when all the stack on doc.ready is complete.
      setTimeout(() => $(".js-select2-companies").select2('data', window.formPrefill), 10);
    }
  })
</script>