HTML JS下拉复选框

时间:2017-09-06 20:24:06

标签: javascript html

初学者在这里 尝试创建一个用户可以选择多个记录的下拉列表 在看完其他人是怎么做的之后,我想出了以下内容。



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;

  }
}

.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;
}

#checkboxes label {
  display: block;
}

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

<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>
      <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>
&#13;
&#13;
&#13;

当我删除STYLE部分时,我得到下拉(没有任何内容)和3创建的复选框选项。 试着把它们放在一起!!

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

&#13;
&#13;
ifelse
&#13;
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;

  }
}
&#13;
.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 {
  
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

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

相关问题