简体   繁体   English

带有多选的 Select2 下拉列表

[英]Select2 dropdown with multiselect

I have made a multiselect dropdown using select 2 and bootstrap.我使用 select 2 和 bootstrap 创建了一个多选下拉列表。
The selected options list is displayed inside the search box.选定的选项列表显示在搜索框中。

I want some help in displaying the selected options list in the div I made.我需要一些帮助来在我制作的 div 中显示选定的选项列表。 Here is the codepen.这是代码笔。

My Codepen我的代码笔

 <div class="card"> <div class="card-header ">Availability</div> <div class="card-body "> <select name="sel" multiple="multiple" class="mul-select" onchange="fun()"> <option value="First">First</option> <option value="Second">Second</option> <option value="Third">Third</option> <option value="Fourth">Fourth</option> <option value="Fifth">Fifth</option> </select> <div class="container display-here"> Display selected options in this div </div> </div> </div>

Please change your code to this hope it helps.请将您的代码更改为此希望它有所帮助。

<div class="card">
  <div class="card-header ">Availability</div>
  <div class="card-body ">

    <select id="multipleSelect" name="sel" multiple="multiple" class="mul-select">

      <option value="First">First</option>
      <option value="Second">Second</option>
      <option value="Third">Third</option>
      <option value="Fourth">Fourth</option>
      <option value="Fifth">Fifth</option>
    </select>
    <div class="container display-here">
    
    </div>
  </div>
</div>

and your script as below.和你的脚本如下。 I have maintained values in an array and then displayed them in the div我在数组中维护了值,然后将它们显示在 div 中

$(document).ready(function() {
    $(".mul-select").select2({
        placeholder: "Select options",
        tags: true,
        closeOnSelect: false

    })
});
var selectArr = [];

$('#multipleSelect').change(function() {

    var idx = $.inArray($(this).val(), selectArr);
    if (idx == -1) {
        selectArr.push($(this).val());
    } else {
        selectArr.splice(idx, 1);
    }
    $.each(selectArr, function(index, value) {
        $('.display-here').text(value);
    });

});

function fun(selectObj) {
    var selectedOptions = $('.mul-select option:selected').length;

    console.log(selectObj.value);


    if (selectedOptions <= 5) $(':input[type="submit"]').prop("disabled", false);
    if (selectedOptions < 1 || selectedOptions > 5) $(':input[type="submit"]').prop("disabled", true);
}


function func() {
    if (document.getElementsByClassName('init')[0].checked === true) console.log(true)
    else console.log(false)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM