JQuery select2从列表中的选项设置默认值?

use*_*276 55 javascript jquery html-select jquery-select2

我希望能够使用JQuery Select2插件设置select元素的默认/选定值.

kri*_*ath 53

还有一种方法 - 只需selected = "selected"select标记中添加一个属性并调用select2它.它必须采用您选择的值.无需额外的JavaScript.像这样 :

标记

<select class="select2">
   <option id="foo">Some Text</option>
   <option id="bar" selected="selected">Other Text</option>
</select>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

$('select').select2(); //oh yes just this!
Run Code Online (Sandbox Code Playgroud)

见小提琴:http://jsfiddle.net/6hZFU/

编辑:(谢谢,Jay Haase!)

如果这不起作用,请尝试将val属性设置select2null,以清除值,如下所示:

$('select').select2("val", null); //a lil' bit more :)
Run Code Online (Sandbox Code Playgroud)

在此之后,设置val为足够简单"Whatever You Want".

  • 我无法让上面的JavaScript为我工作.我确实得到以下工作:$("#id").select2("val",null); (3认同)

use*_*276 23

实现这一目标的一种方法是......

$('select').select2().select2('val', $('.select2 option:eq(1)').val());
Run Code Online (Sandbox Code Playgroud)

所以基本上你首先将插件初始化,然后使用'val'参数指定默认值.实际值取自指定选项,在本例中为#1.因此,此示例中的选定值将为"bar".

<select class=".select2">
  <option id="foo">Some Text</option>
  <option id="bar">Other Text</option>
</select>
Run Code Online (Sandbox Code Playgroud)

希望这对其他人有用.

  • 你的html标记中有一个带有`.`的类.所以基本上你的代码不会设置值,因为你选择了错误的元素. (2认同)
  • 这很好用,但是如何使它选择多个选项,而不仅仅是1个? (2认同)

raj*_*_kw 23

$("#id").select2("val", null); //this will not work..you can try
Run Code Online (Sandbox Code Playgroud)

你应该真的这样做...初始化然后设置值..这也是它对我有用的方式.

$("#id").select2().select2("val", null);
$("#id").select2().select2("val", 'oneofthevaluehere');
Run Code Online (Sandbox Code Playgroud)

  • 这里没有快乐。不管有没有初始化,这根本不适用于 AJAX 数据! (3认同)

小智 15

以上解决方案对我不起作用,但Select2自己的网站上的代码做了:

$('select').val('US'); // Select the option with a value of 'US'
$('select').trigger('change'); // Notify any JS components that the value changed
Run Code Online (Sandbox Code Playgroud)

网页在这里找到

希望这对任何正在挣扎的人都有帮助,就像我一样.

  • 对于未来的访问者,你可以将它组合成一个命令:`$('select').val('US').trigger('change');`,或者甚至更短的`$('select'). .VAL( '美国')变化​​();` (4认同)

Wis*_*ter 8

对于4.x版本

$('#select2Id').val(__INDEX__).trigger('change');
Run Code Online (Sandbox Code Playgroud)

INDEX选择值

$('#select2Id').val('').trigger('change');
Run Code Online (Sandbox Code Playgroud)

什么都不选(如果是的话显示占位符)


Bra*_*n90 5

来自未来?寻找 ajax 源默认值?

// Set up the Select2 control
$('#mySelect2').select2({
    ajax: {
        url: '/api/students'
    }
});

// Fetch the preselected item, and add to the control
var studentSelect = $('#mySelect2');
$.ajax({
    type: 'GET',
    url: '/api/students/s/' + studentId
}).then(function (data) {
    // create the option and append to Select2
    var option = new Option(data.full_name, data.id, true, true);
    studentSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    studentSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

别客气。

参考:https : //select2.org/programmatic-control/add-select-clear-items#preselecting-options-in-an-remotely-sourced-ajax-select2


Sou*_*rik 5

第 1 步:您需要option在选择标签中附加一个带有空白值的空白。

步骤 2:在 select 标签中添加data-placeholder带有占位符值的属性

超文本标记语言

<select class="select2" data-placeholder='--Select--'>
  <option value=''>--Select--</option>
  <option value='1'>Option 1</option>
  <option value='2'>Option 2</option>
  <option value='3'>Option 3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

jQuery

$('.select2').select2({
    placeholder: $(this).data('placeholder')
});
Run Code Online (Sandbox Code Playgroud)

或者

$('.select2').select2({
    placeholder: 'Custom placeholder text'
});
Run Code Online (Sandbox Code Playgroud)