白嫖的html知识 - 怎样在select元素里使用checkbox元素

老金 老金 2022.6.17 10:45 浏览(601) 评论 (0)

像这种Excel的下拉勾选框是非常方便的,但是Html只有单独的checkbox和 select, 没有两者结合使用的元素,那要怎么办呢?

最近公司项目上需要用到这个功能,于是我在网上找了一下,结果给我找到了一个不错的方案,主要使用HTML, CSS and 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;
          }
        }
.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>

 

最后的效果:

enter image description here

 

原文解释:

Explanation:

At first we create a select element that shows text "Select an option", and empty element that covers (overlaps) the select element (<div class="overSelect">). We do not want the user to click on the select element - it would show an empty options. To overlap the element with other element we use CSS position property with value relative | absolute.

To add the functionality we specify a JavaScript function that is called when the user clicks on the div that contains our select element (<div class="selectBox" onclick="showCheckboxes()">).

We also create div that contains our checkboxes and style it using CSS. The above mentioned JavaScript function just changes <div id="checkboxes"> value of CSS display property from "none" to "block" and vice versa.

The solution was tested in the following browsers: Internet Explorer 10, Firefox 34, Chrome 39. The browser needs to have JavaScript enabled.

 

Google翻译

首先,我们创建一个显示文本“Select an option”的选择元素,以及覆盖(重叠)选择元素的空元素( <div class="overSelect">)。 我们不希望用户点击选择元素——它会显示一个空选项。 为了将元素与其他元素重叠,我们使用 CSS 位置属性,其值为 relative | 绝对。

为了添加功能,我们指定了一个 JavaScript 函数,当用户单击包含我们的选择元素的 div 时调用该函数( <div class="selectBox" onclick="showCheckboxes()">).

我们还创建包含复选框的 div 并使用 CSS 设置样式。 上面提到的 JavaScript 函数只是改变了 <div id="checkboxes">CSS display 属性的值从“none”到“block”,反之亦然。

该解决方案在以下浏览器中进行了测试:Internet Explorer 10、Firefox 34、Chrome 39。浏览器需要启用 JavaScript。 

 最后附上原文地址:https://stackoverflow.com/questions/17714705/how-to-use-checkbox-inside-select-option

本文链接 https://www.mangoxo.com/blog/OozeEa5g 版权所有,转载请保留地址链接,感谢!

老金 关注
善良的程序宅
  • 93 动态
  • 0 相册
  • 3 粉丝
  • 11 获赞