Select2 doesn't show selected value

笑着哭i 提交于 2019-11-28 06:15:24

You need to use the initSelection option to set the initial value.

If you are using a pre-defined select element to create the select2, you can use the following method

$('select').select2().select2('val','3')

Demo: Fiddle

add a trigger change after setting val:

$('#my_id').val('3').trigger('change');
Raj Soni

A very simple way to tackle this problem is :

//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);

//Step2: Now reinitialize your select box

//This will work, if you haven't initialized selectbox
$('#my_id').select2(); 

or

//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();

This may help:

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

You can find more on details here:

Thanks

Per here initSelection is deprecated in Select2 4.0 and later.

Using Select2 4.0.0 this worked for me:

$('#my_id').select2({val:3});

HT: @Kokizzu

Here is how to make val in select2 just select the corresponding element. For some reason, select2 doesn't provide the function to look up selections by id.

init:

$("#thing").select2({data:sources, initSelection: function(item, callback) {
  // despite select2 having already read the whole sources list when you 
  // do .val(n) you have to explicitly tell it how to find that item again.
  var to_be_selected = null;
  $.each(sources, function(index, thing) {
    if (thing.id == item.val()) {
      to_be_selected = thing;
      return;
    }
  })
  callback(to_be_selected);
}})

normal code

// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
mmary

I meet with the same problem,this works for me:

Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")

For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -

$(".select").select2({
    data: data_names
});
data_names.forEach(function(name) {
    if (name.selected) {
        $(".select").select2('val', name.id);
    }
});

I had a multiple select2 box with multiple selections.

Step 1 was to put my string of school_ids into an array of integers corresponding to the id of each school, and remove a leading zero. I had to do this in a separate script tag.

<script>
    var school_ids = <%= raw JSON.parse(@search.school_ids).map{|x| x.to_i} - [0]%>
</script>

Step two was to create the select box, then set the values, then trigger like so:

<script>
$(document).ready(function() {
        $('.select2-multiple').select2({
            placeholder: "Hit Enter After Selection",
            width: 'resolve'
        });
        $('.select2-multiple').val(school_ids);
        $('.select2-multiple').trigger('change');
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!