【问题标题】:Clear default selected Value `Bootstrap Select2`清除默认选择的值`Bootstrap Select2`
【发布时间】:2018-05-29 15:08:06
【问题描述】:

我正在使用bootstrap select2,我需要处理此选择,以便在选择其他选项时清除默认选择值null

HTML 代码

<select class="selectpicker" multiple="" name="maritalstatus[]" id="marital-status">
    <option selected="selected" value="">Does Not Matter</option>
    <option value="unmarried">Unmarried</option>
    <option value="divorce">Divorced</option>
    <option value="widow">Widowed</option>
    <option value="awaited-divorce">Awaiting Divorce</option>
</select>

JSCode

$("select").select2({
    theme: "bootstrap",
    containerCssClass: ':all:',
    dropdownCssClass: 'no-search',
    allowClear: true,
});

用户界面

现在根据 UI,如果我选择除 Does Not Matter 以外的任何选项,则应始终清除 Does Not Matter

规则:

  • Does Not Matter 选项不能与任何其他选项一起存在。

【问题讨论】:

    标签: jquery twitter-bootstrap select


    【解决方案1】:

    我设法用以下解决方案解决了它..

    removeDefaultSelect2Value: function() {
      const selectIDs = "#id1, #id2";
      $(selectIDs).on("select2:select", function(e) {
        var $el = $(this);
        var data = e.params.data;
        //console.log(data.element);
        if (data.id === "") {
          $(this).select2("val", 0, true);
        } else {
          setTimeout(function() {
            $el
              .next("span")
              .find(
                'li[title="Does Not Matter"] .select2-selection__choice__remove'
              )
              .click();
          }, 10);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 2020-04-07
      • 2015-04-27
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多