我在使用HTML、CSS和JavaScript创建自定义下拉列表时遇到了一个问题.我的目标是在 Select 占位符中显示选中的复选框的值.当用户点击任何复选框时,我希望相应的文本被追加到占位符.

有人能指导我正确的方法或提供一个代码示例来实现这一点吗?为了更详细地了解,我附上了一张想要的结果的图像.

Required Outcome is: click here to see the image

document.addEventListener('DOMContentLoaded', function() {
  function initializeCustomDropdown(containerSelector) {
    var container = document.querySelector(containerSelector);
    container.querySelectorAll('.custom-dropdown-onclick').forEach(function(filter) {
      filter.addEventListener('click', function() {
        var dropdown = this.nextElementSibling;
        dropdown.style.display = (dropdown.style.display === 'none' || dropdown.style.display === '') ? 'block' : 'none';
      });
    });

    document.addEventListener('click', function(event) {
      if (!event.target.closest(".custom-dropdown-select, .custom-dropdown-area")) {
        container.querySelectorAll(".custom-dropdown-area").forEach(function(dropdown) {
          dropdown.style.display = 'none';
        });
      }
    });

    container.querySelectorAll(".custom-dropdown-all").forEach(function(checkbox) {
      checkbox.addEventListener('change', function() {
        var group = this.dataset.group;
        var itemSelected = [];
        container.querySelectorAll(group).forEach(function(checkbox) {
          checkbox.checked = this.checked;
        }, this);

      });
    });

    container.querySelectorAll('.custom-dropdown-child').forEach(function(checkbox) {
      checkbox.addEventListener('change', function() {
        var itemSelected = [];
        container.querySelectorAll('.custom-dropdown-child:checked').forEach(function(checkbox) {
          itemSelected.push(checkbox.value);
        });

        if (container.querySelectorAll('.custom-dropdown-child:checked').length === container.querySelectorAll('.custom-dropdown-child').length) {
          container.querySelector(".custom-dropdown-all").checked = true;
        } else {
          container.querySelector(".custom-dropdown-all").checked = false;
        }
      });
    });
  }

  // Usage example
  initializeCustomDropdown('.custom-dropdown-one');
});
.custom-dropdown-select {
  position: relative;
  border: 1px solid #999999;
  border-radius: 3px;
  float: left;
  max-width: 200px;
  width: 100%;
  background: #ffffff;
  padding: 5px 10px;
  margin: 5px;
  height: 20px;
}

.hide {
  display: none
}

.custom-dropdown-select-arrow {
  position: absolute;
  top: 17px;
  right: 5px;
  border-top: 6px solid #666666;
  border-left: 5px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 5px solid transparent;
  z-index: 1;
}

.custom-dropdown-onclick {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  background: transparent;
  cursor: pointer;
  z-index: 1;
  height: 40px;
}

.custom-dropdown-area {
  background: #ffffff;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.24);
  position: absolute;
  width: 100%;
  padding: 10px 0;
  top: 38px;
  left: 0;
  z-index: 2;
}

.custom-dropdown-area .custom-dropdown-list {
  padding: 0 !important;
  margin: 0 !important;
}

.custom-dropdown-area .custom-dropdown-list li {
  list-style: none;
}
<div class="custom-dropdown-select custom-dropdown-one">
  <span class="custom-dropdown-placeholder selected-items-placeholder">All</span>
  <span class="custom-dropdown-select-arrow "></span>
  <span class="custom-dropdown-onclick"></span>
  <div class="custom-dropdown-area hide">
    <div>
      <input id="select-all" type="checkbox" class="custom-dropdown-all" data-group=".custom-dropdown-child">
      <label for="select-all" class="text-black">All</label><br>
      <ul class="custom-dropdown-list">
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Office">
          <label>Office</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Land">
          <label>Land</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Medical">
          <label>Medical</label>
        </li>
        <li><input type="checkbox" class="custom-dropdown-child" d name="item" value="Industrial">
          <label>Industrial</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Flex">
          <label>Flex</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Specialty">
          <label>Specialty</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Medical Office">
          <label>Medical Office</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Retail">
          <label>Retail</label>
        </li>
        <li>
          <input type="checkbox" class="custom-dropdown-child" name="item" value="Build to Suite">
          <label>Build to Suite</label>
        </li>
      </ul>
    </div>

  </div>
</div>

推荐答案

我已经修改了你的代码,希望它能满足你的要求

我已经创建了一个处理占位符文本的css类,下面是css代码:

.custom-dropdown-placeholder {
    display: block;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

有一个编写占位符文本的Java脚本修改,使用itemSelected.join(',')判断下面的Java脚本代码并阅读注释,以理解代码以及我实际应该做的事情

function initializeCustomDropdown(containerSelector) {
    ....
    ....


    var placeholder = container.querySelector(".selected-items-placeholder");

    container.querySelectorAll(".custom-dropdown-all").forEach(function (checkbox) {
        checkbox.addEventListener('change', function () {
            ....
            ....
            // added if else 
            if (this.checked) { // if parent All checkbox checked then write All
                placeholder.innerHTML = 'All';
            } else { // loop on all checked checkboxes and push into an array and then write with innHTML
                container.querySelectorAll(group + ':checked').forEach(function (checkbox) {
                    itemSelected.push(checkbox.value);
                });
                placeholder.innerHTML = itemSelected.join(',');
            }

        });
    });

    container.querySelectorAll('.custom-dropdown-child').forEach(function (checkbox) {
        checkbox.addEventListener('change', function () {
            ....
            ....

            if (container.querySelectorAll('.custom-dropdown-child:checked').length === container.querySelectorAll('.custom-dropdown-child').length) {
                container.querySelector(".custom-dropdown-all").checked = true;

                placeholder.innerHTML = 'All'; // added this line to write the All if all checkboxes are checked

            } else {
                container.querySelector(".custom-dropdown-all").checked = false;

                placeholder.innerHTML = itemSelected.join(','); // added this line to write checked values with comma separately
            }
        });
    });
}

There is a live snippet below to see the result.

document.addEventListener('DOMContentLoaded', function () {


           
            function initializeCustomDropdown(containerSelector) {

                var container = document.querySelector(containerSelector);


                container.querySelectorAll('.custom-dropdown-onclick').forEach(function (filter) {
                    filter.addEventListener('click', function () {
                        var dropdown = this.nextElementSibling;
                        dropdown.style.display = (dropdown.style.display === 'none' || dropdown.style.display === '') ? 'block' : 'none';
                    });
                });

                document.addEventListener('click', function (event) {
                    if (!event.target.closest(".custom-dropdown-select, .custom-dropdown-area")) {
                        container.querySelectorAll(".custom-dropdown-area").forEach(function (dropdown) {
                            dropdown.style.display = 'none';
                        });
                    }
                });



                var placeholder = container.querySelector(".selected-items-placeholder");

                container.querySelectorAll(".custom-dropdown-all").forEach(function (checkbox) {
                    checkbox.addEventListener('change', function () {
                        var group = this.dataset.group;
                        var itemSelected = [];

                        container.querySelectorAll(group).forEach(function (checkbox) {
                            checkbox.checked = this.checked;
                        }, this);

                        if (this.checked) {
                            placeholder.innerHTML = 'All';
                        } else {
                            container.querySelectorAll(group + ':checked').forEach(function (checkbox) {
                                itemSelected.push(checkbox.value);
                            });
                            placeholder.innerHTML = itemSelected.join(',');
                        }
                    });
                });

                container.querySelectorAll('.custom-dropdown-child').forEach(function (checkbox) {
                    checkbox.addEventListener('change', function () {
                        var itemSelected = [];
                        container.querySelectorAll('.custom-dropdown-child:checked').forEach(function (checkbox) {
                            itemSelected.push(checkbox.value);
                        });

                        if (container.querySelectorAll('.custom-dropdown-child:checked').length === container.querySelectorAll('.custom-dropdown-child').length) {
                            container.querySelector(".custom-dropdown-all").checked = true;
                            placeholder.innerHTML = 'All';
                        } else {
                            container.querySelector(".custom-dropdown-all").checked = false;
                            placeholder.innerHTML = itemSelected.join(',');
                        }
                    });
                });
            }

            // Usage example
            initializeCustomDropdown('.custom-dropdown-one');

        });
.custom-dropdown-select {
            position: relative;
            border: 1px solid #999999;
            border-radius: 3px;
            float: left;
            max-width: 200px;
            width: 100%;
            background: #ffffff;
            padding: 5px 10px;
            margin: 5px;
            height: 20px;
        }

        .hide {
            display: none
        }

        .custom-dropdown-select-arrow {
            position: absolute;
            top: 17px;
            right: 5px;
            border-top: 6px solid #666666;
            border-left: 5px solid transparent;
            border-bottom: 6px solid transparent;
            border-right: 5px solid transparent;
            z-index: 1;
        }

        .custom-dropdown-onclick {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            background: transparent;
            cursor: pointer;
            z-index: 1;
            height: 40px;
        }

        .custom-dropdown-area {
            background: #ffffff;
            box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.24);
            position: absolute;
            width: 100%;
            padding: 10px 0;
            top: 38px;
            left: 0;
            z-index: 2;
        }

        .custom-dropdown-area .custom-dropdown-list {
            padding: 0 !important;
            margin: 0 !important;
        }

        .custom-dropdown-area .custom-dropdown-list li {
            list-style: none;
        }

        .custom-dropdown-placeholder {
            display: block;
            width: 100%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
<div class="custom-dropdown-select custom-dropdown-one">
        <span class="custom-dropdown-placeholder selected-items-placeholder">All</span>
        <span class="custom-dropdown-select-arrow "></span>
        <span class="custom-dropdown-onclick"></span>
        <div class="custom-dropdown-area hide">
            <div>
                <input id="select-all" type="checkbox" class="custom-dropdown-all" data-group=".custom-dropdown-child">
                <label for="select-all" class="text-black">All</label><br>
                <ul class="custom-dropdown-list">
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Office">
                        <label>Office</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Land">
                        <label >Land</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Medical">
                        <label >Medical</label>
                    </li>
                    <li><input type="checkbox" class="custom-dropdown-child" d name="item" value="Industrial">
                        <label >Industrial</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item"  value="Flex">
                        <label >Flex</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item"  value="Specialty">
                        <label >Specialty</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Medical Office">
                        <label >Medical Office</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Retail">
                        <label >Retail</label>
                    </li>
                    <li>
                        <input type="checkbox" class="custom-dropdown-child" name="item" value="Build to Suite">
                        <label >Build to Suite</label>
                    </li>
                </ul>
            </div>

        </div>
    </div>

Javascript相关问答推荐

可以将SuperTest导入为ES 6模块吗?

导入图像与内联包含它们NextJS

使用Apps Script缩短谷歌表中的URL?

设置默认值后动态更新japbox或调色板

不渲染具有HTML参数的React元素

有条件的悲剧

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

为什么当我解析一个promise时,输出处于挂起状态?

CheckBox作为Vue3中的一个组件

当Redux提供程序访问Reduxstore 时,可以安全地从Redux提供程序外部调用钩子?

如何将Cookie从服务器发送到用户浏览器

我的角模板订阅后不刷新'

单个HTML中的多个HTML文件

使用Ace编辑器对子组件实例的native-element 进行Angular 获取时面临的问题

使用Document.Evaluate() Select 一个包含撇号的HTML元素

将基元传递给THEN处理程序

更改agGRID/Reaction中的单元格格式

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?

JavaScript&;Reaction-如何避免在不使用字典/对象的情况下出现地狱?

为什么我不能使用其同级元素调用和更新子元素?