Bootstrap 4 multiselect dropdown

女生的网名这么多〃 提交于 2019-11-28 05:17:50

Because the bootstrap-select is a bootstrap component and therefore you need to include it in your code as you did for your V3

NOTE: this component only works in since version 1.13.0

$('select').selectpicker();
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>



<select class="selectpicker" multiple data-live-search="true">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Relish</option>
</select>

MultiSelect DropDown with checkboxes

<div class="col-md-6">
                                        <div class="depSelectToEdit">
                                            <label>Departments: <input type="text" class="total" value="6 Selected" readonly></label>
                                            <div class="form-control parentChkBox">
                                                <div class="checkbox">
                                                    <label><input class="parentChk" type="checkbox" value="All">Select All</label>
                                                </div>
                                            </div>
                                            <div class="form-control multiSelections">
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="Networking" checked>Networking</label>
                                                </div>
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="Billing" checked>Billing</label>
                                                </div>
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="Support" checked>Support</label>
                                                </div>
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="IT" checked>IT</label>
                                                </div>
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="Marketing" checked>Marketing</label>
                                                </div>
                                                <div class="checkbox">
                                                    <label><input class="childChk" type="checkbox" value="Management" checked>Management</label>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

Script

 //  Department selection In EditPopup
                                        $('.depSelectToEdit .parentChk').click(function () {
                                            if ($(this).is(':checked')) {
                                                $(".depSelectToEdit .total").val("");
                                                $(".depSelectToEdit .total").val("Selected " + $('.depSelectToEdit .parentChk').val());
                                                $(".depSelectToEdit .childChk").prop('checked', true);

                                                $('.depSelectToEdit .childChk').click(function () {
                                                    var totalchildChk = $(".depSelectToEdit .childChk").length;
                                                    if ($(".depSelectToEdit .childChk:checked").length < totalchildChk) {
                                                        $('.depSelectToEdit .parentChk').prop('checked', false);
                                                    } else
                                                    {
                                                        $('.depSelectToEdit .parentChk').prop('checked', true);
                                                        $(".depSelectToEdit .total").val("Selected " + $('.depSelectToEdit .parentChk').val());
                                                    }
                                                });
                                            } else {
                                                $(".depSelectToEdit .childChk").prop('checked', false);
                                                $(".depSelectToEdit .total").val("");
                                            }
                                        });


                                        $('.depSelectToEdit .childChk').click(function () {
                                            $(".depSelectToEdit .total").val('');
                                            $(".depSelectToEdit .childChk").each(function () {
                                                if ($(".depSelectToEdit .childChk:checked").length < 4) {
                                                    if ($(this).prop('checked')) {
                                                        var childChkfirstValue = $(".depSelectToEdit .total").val();
                                                        if (childChkfirstValue == "") {
                                                            var allValues = childChkfirstValue + $(this).val();
                                                        } else
                                                        {
                                                            var allValues = childChkfirstValue + ", " + $(this).val();
                                                        }
                                                        $(".depSelectToEdit .total").val(allValues);
                                                    }
                                                } else
                                                {
                                                    var count = $('.depSelectToEdit .childChk:checked').length;
                                                    $(".depSelectToEdit .total").val(count + " Selected");
                                                }
                                            });

                                        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!