我如何设置占位符的值重置由select2。在我的例子中,如果位置或等级选择框被单击,我的select2有一个值,那么select2的值应该重置并显示默认占位符。这个脚本正在重置值,但不会显示占位符

$("#locations, #grade ").change(function() {
   $('#e6').select2('data', {
     placeholder: "Studiengang wählen",
     id: null,
     text: ''
   });
});

$("#e6").select2({
   placeholder: "Studiengang wählen",
   width: 'resolve',
   id: function(e) {
     return e.subject;
   },
   minimumInputLength: 2,
   ajax: {
     url: "index.php?option=com_unis&task=search.locator&tmpl=component&<?php echo JSession::getFormToken() ?>=1",
     dataType: 'json',
     data: function(term, page) {
       return {
         q: term, // search term
         g: $('#grade option:selected').val(),
         o: $('#locations option:selected').val()
       };
     },
     results: function(data, page) {
       return {
         results: data
       };
     }
   },
   formatResult: subjectFormatResult,
   formatSelection: subjectFormatSelection,
   dropdownCssClass: "bigdrop",
   escapeMarkup: function(m) {
     return m;
   }
});

当前回答

与Select2版本4.0.9这适用于我:

$( "#myselect2" ).val('').trigger('change');

其他回答

$('#select_id').select2('data', null);

重置select2并显示占位符

用这个:

$('.select').val([]).trigger('change');

Select2使用了一个特定的CSS类,所以重置它的简单方法是:

$('.select2-container').select2('val', '');

这样做的好处是,如果在同一个表单中有多个Select2,它们都会被这个命令重置。

删除所选值

$('#employee_id').select2('val','');

清除选项值

$('#employee_id').html('');

我使用select2版本select2 4.0.5 $ (' .classToClear ') .select2 () .val(“);