如何在下拉列表HTML(rails)中搜索项目并选择该项目?

时间:2018-03-02 22:52:16

标签: html css ruby-on-rails

我正在尝试使用用户选择项目的表单的下拉菜单。到目前为止,我只能使用<a href=...之类的链接来完成这项工作,但似乎无法让用户选择要在表单中提交的项目。如何换掉这个链接?下面是一段代码。

FYI这应该是用户从下拉菜单中选择项目(具有搜索功能)的输入。这只是用户回答的几个问题之一。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd}

.show {display:block;}
</style>
</head>
<body>

<h2>Select Item</h2>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#item1">Item 1</a>
    <a href="#item2">Item 2</a>
    <a href="#item3">Item 3</a>
  </div>
</div>

<script>

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}
</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您想要的是复选框,因为复选框是选项,将随表单一起提交。您可以将HTML更改为

<h2>Select Item</h2>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <input id="item-1" name="item-1" type="checkbox" />
    <label for="item-1">Item 1</label>
    <input id="item-2" name="item-2" type="checkbox" />
    <label for="item-2">Item 2</label>
    <input id="item-3" name="item-3" type="checkbox" />
    <label for="item-3">Item 2</label>
  </div>
</div>

然后只需将CSS更改为:

.dropdown-content input[type="checkbox"] {
  opacity: 0.01;
  position: absolute;
}

.dropdown-content input[type="checkbox"]:checked + label {
  background-image: url('http://www.clker.com/cliparts/u/e/E/I/2/i/dark-green-check-mark-md.png');
  background-position: center right 10px;
  background-repeat: no-repeat;
  background-size: 15px auto;
}

.dropdown-content label {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown label:hover {background-color: #ddd}

现在结构上可能会有一些调整,但这取决于你。

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
}

.show {display:block;}

.dropdown-content input[type="checkbox"] {
  opacity: 0.01;
  position: absolute;
}

.dropdown-content input[type="checkbox"]:checked + label {
  background-image: url('http://www.clker.com/cliparts/u/e/E/I/2/i/dark-green-check-mark-md.png');
  background-position: center right 10px;
  background-repeat: no-repeat;
  background-size: 15px auto;
}

.dropdown-content label {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown label:hover {background-color: #ddd}
<h2>Select Item</h2>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <input id="item-1" name="item-1" type="checkbox" />
    <label for="item-1">Item 1</label>
    <input id="item-2" name="item-2" type="checkbox" />
    <label for="item-2">Item 2</label>
    <input id="item-3" name="item-3" type="checkbox" />
    <label for="item-3">Item 2</label>
  </div>
</div>