【问题标题】:Put select2 tags to the right将select2标签放在右边
【发布时间】:2018-08-23 19:38:10
【问题描述】:

我在下面的 sn-p 中使用 jquery 和 select2 创建了一个标签输入。

$(document).ready(function() {
  
  $("#tag-input .js-example-basic-multiple").select2({
    placeholder: "Add tag"
  }).on('change', function(e) {
    if($(this).val() && $(this).val().length) {
      $(this).next('.select2-container').find('li.select2-search--inline input.select2-search__field').attr('placeholder', 'Add tag');
    }
  });
});
.select2-selection {
  height: 36px !important; 
  font-size: 13px;
  font-family: 'Open Sans', sans-serif;
  border-radius: 0 !important;
  border: solid 1px #D1D1D1 !important;
  padding-left: 4px;
}

.select2-selection--multiple {
  height: 160px !important;
}

.select2-selection__choice__remove {
   float: right;
   margin-right: 0;
   margin-left: 2px;
}

.select2-selection__choice {
  height: 40px;
  line-height: 40px;
  padding-right: 12px !important;
  padding-left: 16px !important;
  background-color: #D0F3FF !important;
  color: #333 !important;
  border: none !important;
  border-radius: 3px !important;
}

.select2-search--inline .select2-search__field {
  line-height: 40px;
  color: #333;
  width: 88px !important;
}

.select2-container {
  
}

.select2-container:hover,
.select2-container:focus,
.select2-container:active,
.select2-selection:hover,
.select2-selection:focus,
.select2-selection:active {
  outline-color: transparent;
  outline-style: none;
}

.select2-results__options li {
  display: block; 
}

.select2-selection__rendered {
  transform: translateY(2px);
}

.select2-selection__arrow {
  display: none;
}

.select2-results__option--highlighted {
  background-color: #D0F3FF !important;
  color: #333 !important;
}

.select2-selection__choice__remove {
  margin-left: 16px;
  font-size: 16px;
}

.select2-dropdown {
  border-radius: 0 !important;
  box-shadow: 0px 3px 6px 0 rgba(0,0,0,0.15) !important;
  border: none !important;
  margin-top: 4px !important;
}

.select2-results__option {
  font-family: 'Open Sans', sans-serif;
  font-size: 13px;
  line-height: 24px !important;
  vertical-align: middle !important;
  padding-left: 8px !important;
}

.selectable-dropdown-each {
  display: block; 
  height: 36px;
  line-height: 36px !important;
  padding-right: 0;
  padding-left: 16px;
  cursor: pointer;
}

.selectable-hover {
  background-color: #D0F3FF;
}

.select2-results__option[aria-selected="true"] {
  background-color: #f1f1f1 !important; 
}

.select2-search__field {
  font-family: 'Open Sans', sans-serif;
  color: #333;
  font-size: 13px;
  padding-left: 8px !important;
  border-color: #D1D1D1 !important;
}

.select2-selection__placeholder {
  color: #D1D1D1 !important; 
}

.form-unit .select2-container, .form-unit .select2-selection {
  width: 412px !important;
}

#tag-input .select2-selection--multiple {
  border: none !important;
  height: auto !important;
  padding: 0 !important;
}

#tag-input .select2-selection__rendered {
  padding: 0 !important; 
}

#tag-input .select2-search__field {
  height: 32px;
  border-radius: 16px;
  background-color: #D0F3FF;
  border: none !important;
  padding-left: 16px !important;
  padding-right: 16px;
  width: 104px !important;
}

#tag-input .select2-search__field::placeholder {
  color: #aaa; 
}

#tag-input .select2-search__field:focus {
  border: solid 1px #00c983 !important;
  background-color: #fff;
}

#tag-input .select2-search__field:focus::placeholder {
  color: #d1d1d1;
}

#tag-input .select2-selection__choice {
  height: 32px;
  line-height: 32px;
  border-radius: 16px !important;
}



.form {
  margin-bottom: 40px;
  margin: 0 auto;
  width: 1302px;
  font-family: 'Open Sans', sans-serif;
}

.form-row {
  display: block;
  width: 100%;
  margin: 0 0 32px 0 !important;
}

.form-divided {
  display: inline-block;
}

.form-divided-left {
  margin-right: 32px;
}

.form-divided-right {
  margin-left: -4px;
  width: 412px;
}

.form-input-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 8px;
  color: #333;
}

.form-unit {
  position: relative;
  vertical-align: top;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">

<div class="form-row">
    <div id="tag-input" class="form-unit form-divided form-divided-right">
      <label for="emp-id" class="form-input-label">Tag Input</label>
      <select class="js-example-basic-multiple" name="states[]" multiple="multiple">
        <option value="a1">Item A1</option>
        <option value="a2">Item A2</option>
        <option value="b1">Item B1</option>
        <option value="a1">Item C1</option>
        <option value="a2">Item C2</option>
        <option value="b1">Item C3</option>
      </select>
    </div>
  </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>

如您所见,输入字段看起来像随着标签的插入而移动。有没有办法将插入的标签放在输入的右侧?这样我的输入就会留在容器的左侧。

任何人都可以帮助我吗?任何帮助表示赞赏! :)

【问题讨论】:

    标签: jquery html css jquery-select2


    【解决方案1】:

    将代码添加到我的previous post 中的change 函数以分离搜索&lt;li&gt; 并将其添加到&lt;ul class="select2-selection__rendered"&gt;,这是将输入标记保留在左侧的更改并继续向右移动选定的选项。

    $("#tag-input .js-example-basic-multiple").select2({
      placeholder: "Add tag"
    }).on('change', function(e) {
        if($(this).val() && $(this).val().length) {
           var searchItem = $(this).next('.select2-container').find('li.select2-search--inline').detach();
    
           var renderList = $(this).next('.select2-container').find('ul.select2-selection__rendered').prepend(searchItem);
           searchItem.find('input.select2-search__field').attr('placeholder', 'Add tag');
    
        }
    });
    

    片段:

    $(document).ready(function() {
      
      $("#tag-input .js-example-basic-multiple").select2({
        placeholder: "Add tag"
      }).on('change', function(e) {
        if($(this).val() && $(this).val().length) {
        	var searchItem = $(this).next('.select2-container').find('li.select2-search--inline').detach();
          
          var renderList = $(this).next('.select2-container').find('ul.select2-selection__rendered').prepend(searchItem);
          searchItem.find('input.select2-search__field').attr('placeholder', 'Add tag');
          
        }
      });
    });
    .select2-selection {
      height: 36px !important; 
      font-size: 13px;
      font-family: 'Open Sans', sans-serif;
      border-radius: 0 !important;
      border: solid 1px #D1D1D1 !important;
      padding-left: 4px;
    }
    
    .select2-selection--multiple {
      height: 160px !important;
    }
    
    .select2-selection__choice__remove {
       float: right;
       margin-right: 0;
       margin-left: 2px;
    }
    
    .select2-selection__choice {
      height: 40px;
      line-height: 40px;
      padding-right: 12px !important;
      padding-left: 16px !important;
      background-color: #D0F3FF !important;
      color: #333 !important;
      border: none !important;
      border-radius: 3px !important;
    }
    
    .select2-search--inline .select2-search__field {
      line-height: 40px;
      color: #333;
      width: 88px !important;
    }
    
    .select2-container {
      
    }
    
    .select2-container:hover,
    .select2-container:focus,
    .select2-container:active,
    .select2-selection:hover,
    .select2-selection:focus,
    .select2-selection:active {
      outline-color: transparent;
      outline-style: none;
    }
    
    .select2-results__options li {
      display: block; 
    }
    
    .select2-selection__rendered {
      transform: translateY(2px);
    }
    
    .select2-selection__arrow {
      display: none;
    }
    
    .select2-results__option--highlighted {
      background-color: #D0F3FF !important;
      color: #333 !important;
    }
    
    .select2-selection__choice__remove {
      margin-left: 16px;
      font-size: 16px;
    }
    
    .select2-dropdown {
      border-radius: 0 !important;
      box-shadow: 0px 3px 6px 0 rgba(0,0,0,0.15) !important;
      border: none !important;
      margin-top: 4px !important;
    }
    
    .select2-results__option {
      font-family: 'Open Sans', sans-serif;
      font-size: 13px;
      line-height: 24px !important;
      vertical-align: middle !important;
      padding-left: 8px !important;
    }
    
    .selectable-dropdown-each {
      display: block; 
      height: 36px;
      line-height: 36px !important;
      padding-right: 0;
      padding-left: 16px;
      cursor: pointer;
    }
    
    .selectable-hover {
      background-color: #D0F3FF;
    }
    
    .select2-results__option[aria-selected="true"] {
      background-color: #f1f1f1 !important; 
    }
    
    .select2-search__field {
      font-family: 'Open Sans', sans-serif;
      color: #333;
      font-size: 13px;
      padding-left: 8px !important;
      border-color: #D1D1D1 !important;
    }
    
    .select2-selection__placeholder {
      color: #D1D1D1 !important; 
    }
    
    .form-unit .select2-container, .form-unit .select2-selection {
      width: 412px !important;
    }
    
    #tag-input .select2-selection--multiple {
      border: none !important;
      height: auto !important;
      padding: 0 !important;
    }
    
    #tag-input .select2-selection__rendered {
      padding: 0 !important; 
    }
    
    #tag-input .select2-search__field {
      height: 32px;
      border-radius: 16px;
      background-color: #D0F3FF;
      border: none !important;
      padding-left: 16px !important;
      padding-right: 16px;
      width: 104px !important;
    }
    
    #tag-input .select2-search__field::placeholder {
      color: #aaa; 
    }
    
    #tag-input .select2-search__field:focus {
      border: solid 1px #00c983 !important;
      background-color: #fff;
    }
    
    #tag-input .select2-search__field:focus::placeholder {
      color: #d1d1d1;
    }
    
    #tag-input .select2-selection__choice {
      height: 32px;
      line-height: 32px;
      border-radius: 16px !important;
    }
    
    
    
    .form {
      margin-bottom: 40px;
      margin: 0 auto;
      width: 1302px;
      font-family: 'Open Sans', sans-serif;
    }
    
    .form-row {
      display: block;
      width: 100%;
      margin: 0 0 32px 0 !important;
    }
    
    .form-divided {
      display: inline-block;
    }
    
    .form-divided-left {
      margin-right: 32px;
    }
    
    .form-divided-right {
      margin-left: -4px;
      width: 412px;
    }
    
    .form-input-label {
      display: block;
      font-size: 12px;
      font-weight: 700;
      margin-bottom: 8px;
      color: #333;
    }
    
    .form-unit {
      position: relative;
      vertical-align: top;
    }
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">
    
    <div class="form-row">
        <div id="tag-input" class="form-unit form-divided form-divided-right">
          <label for="emp-id" class="form-input-label">Tag Input</label>
          <select class="js-example-basic-multiple" name="states[]" multiple="multiple">
            <option value="a1">Item A1</option>
            <option value="a2">Item A2</option>
            <option value="b1">Item B1</option>
            <option value="a1">Item C1</option>
            <option value="a2">Item C2</option>
            <option value="b1">Item C3</option>
          </select>
        </div>
      </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>

    希望这会有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2014-05-26
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多