将select2标签放在右边

时间:2018-08-23 19:38:10

标签: jquery html css jquery-select2

我在下面的代码段中使用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>

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

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

1 个答案:

答案 0 :(得分:1)

从我的previous postchange函数中添加代码以分离搜索<li> 并将其添加到<ul class="select2-selection__rendered">之前,这是更改将输入标签保留在左侧,并继续将所选选项向右移动。

$("#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>

希望这会有所帮助。 :)