【问题标题】:How to search for an item and select that item within a dropdown HTML(rails)?如何在下拉 HTML(rails)中搜索项目并选择该项目?
【发布时间】:2018-03-02 22:52:15
【问题描述】:

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

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

<!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>

【问题讨论】:

    标签: html css ruby-on-rails


    【解决方案1】:

    您想要的是复选框,因为复选框是选项,将与表单一起提交。您可以将 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>

    【讨论】:

    • 嗨,杰瑞,感谢您的回答。从下拉列表中选择一个元素后,有没有办法将其显示为输入或标签(也许将“下拉”替换为所选项目的名称?这样用户无需再次单击按钮即可看到他们选择的内容.
    • 您要做的是监听复选框的更改,然后在发生时更改下拉列表的内容:jsfiddle.net/4vaervkj/20
    • 其实很抱歉,我会将其存储在其他地方,因为您想保留输入值,jsfiddle.net/4vaervkj/23
    • 我看了看剧本。如果用户想改变他们的反应怎么办?
    • 你可以做的不是隐藏下拉菜单。该值可以放置在您想要的任何位置。因此,您可以将其附加到下拉按钮或使用不同的元素来显示所选结果。
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多