选择框的折叠下拉列表

时间:2018-11-12 19:49:28

标签: javascript jquery html css

我正在处理“自定义选择”下拉列表,该下拉列表具有从该question获得的多个选择的复选框。

问题在于下拉列表一旦展开,当您在外部的任意位置单击时,它不会像常规选择框中那样折叠。

HTML:

    <form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
    </div>
    <div id="checkboxes">
      <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
                <span class="caret"> </span>
      <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
      <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>


    </div>
  </div>
</form>

CSS:

    .multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
  height: 100px;

}

#checkboxes label {
  display: block;


}

#checkboxes label:hover {
  background-color: #1e90ff;
}

.caret {
    border-top: 4px solid #919da9;
}

JavaScript:

    var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}

这里是Fiddle

仅当您单击选择时,它才会折叠,原因可能是因为它没有任何选项。相反,它具有输入类型。

1 个答案:

答案 0 :(得分:2)

click上为document添加一个事件监听器。然后在该侦听器中,检查单击是否发生在下拉菜单或选项上,如果发生,则什么都不做(因此用户仍然可以打开下拉菜单并选择选项),否则隐藏选项:

var expanded = false,
    checkboxes = document.getElementById("checkboxes");

function showCheckboxes() {
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}

document.addEventListener("click", function(event) {
  // If click on dropdown/options do nothing
  if (event.target.closest(".selectBox, #checkboxes")) return;
  // Otherwise hide the options
  checkboxes.style.display = "none";
});
.multiselect {
  width: 200px;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
  height: 100px;
 
}

#checkboxes label {
  display: block;
  
  
}

#checkboxes label:hover {
  background-color: #1e90ff;
}

.caret {
    border-top: 4px solid #919da9;
}
<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
      <select>
        <option>Select an option</option>
      </select>
      <div class="overSelect"></div>
    </div>
    <div id="checkboxes">
      <label for="one">
        <input type="checkbox" id="one" />First checkbox</label>
                <span class="caret"> </span>
      <label for="two">
        <input type="checkbox" id="two" />Second checkbox</label>
      <label for="three">
        <input type="checkbox" id="three" />Third checkbox</label>

  
    </div>
  </div>
</form>