清除select2中的自动完成功能而不关闭下拉列表

时间:2018-05-20 07:07:11

标签: jquery autocomplete jquery-select2

有没有人知道如何在select2下拉列表中清除自动填充字段的内容?用户必须退格以清除文本,这可能会在需要多次搜索/选择时烦人。一个简单的' X'或者(如果不可能),该字段旁边的按钮会很好。我已尝试.select2-search--dropdown .select2-search__field的各种组合尝试清除搜索字段,但无处可去,下拉列表始终关闭。小提琴如下所示。感谢。

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"></script>

<body class='bodyClass'>
  <div class="row">
    <select id="SteelList" name="SteelList" class="form-control select2-multiple">
      <option value="40">France</option>
      <option value="960">Spain</option>
      <option value="540">Italy</option>
    </select>
  </div>
</body>



  $(function() {

  var S2MultiCheckboxes = function(options, element) {
    var self = this;
    self.options = options;
    self.$element = $(element);
    var values = self.$element.val();
    self.$element.removeAttr('multiple');
    self.select2 = self.$element.select2({

      allowClear: true, // Puts little 'X' in top placeholder  
      //minimumResultsForSearch: -1,
      placeholder: options.placeholder,
      closeOnSelect: false,

      templateSelection: function() {
        return self.options.templateSelection(self.$element.val() || [], $('option', self.$element).length);
      },
      templateResult: function(result) {
        if (result.loading !== undefined)
          return result.text;
        return $('<div>').text(result.text).addClass(self.options.wrapClass);
      }
    }).data('select2');
    self.select2.$results.off("mouseup").on("mouseup", ".select2-results__option[aria-selected]", (function(self) {
      return function(evt) {
        var $this = $(this);

        var data = $this.data('data');

        if ($this.attr('aria-selected') === 'true') {
          self.trigger('unselect', {
            originalEvent: evt,
            data: data
          });
          return;
        }

        self.trigger('select', {
          originalEvent: evt,
          data: data
        });
      }
    })(self.select2));
    self.$element.attr('multiple', 'multiple').val(values).trigger('change.select2');
  }

  // ------------------------

  $.fn.extend({
    select2MultiCheckboxes: function() {
      var options = $.extend({
        placeholder: 'Choose elements',
        templateSelection: function(selected, total) {
          return selected.length + ' > ' + total + ' total';
        },
        wrapClass: 'wrap'
      }, arguments[0]);

      this.each(function() {
        new S2MultiCheckboxes(options, this);
      });
    }
  });

})

// ======================================

// Init script
$(function() {
  $('.select2-multiple').select2MultiCheckboxes({

    closeOnSelect: true,

    templateSelection: function(selected, total) {
      return "Select Country - Selected " + selected.length + " of " + total;
    }

  })

});




 .bodyClass {
 min-height: 100vh;
}

.select2-results__option .wrap:before {
  font-family: fontAwesome;
  color: #999;
  content: "\f096";
  width: 25px;
  height: 25px;
  padding-right: 10px;
}

.select2-results__option[aria-selected=true] .wrap:before {
  content: "\f14a";
}

.select2-container--default .select2-search--dropdown .select2-search__field {
  border: 1px solid red;
}

.row {
  padding: 10px;
}

.select2-multiple,
.select2-multiple2 {
  width: 50%
}

.select2-results__group .wrap:before {
  display: none;
}

小提琴:https://jsfiddle.net/3hs4cf4n/

1 个答案:

答案 0 :(得分:1)

找到了一个清除select2自动完成字段的答案。这不是您所需要的完整答案,但可以帮助您找到解决方案。

要清除ID为=“ name”的文件,请使用以下代码

$('#name').val(null).trigger('change');