【问题标题】:Drop Down Menu/Text Field in one下拉菜单/文本字段合二为一
【发布时间】:2013-08-20 23:16:49
【问题描述】:

我正在构建新网站,我需要一个下拉菜单来选择我网站中的内容数量。但同时我需要这个下拉列表来接受文本。因此,如果客户想从下拉列表中进行选择,那么他可以,如果客户想通过文本输入金额,那么他也可以。如您所见,我想让它成为双重的。

例如:假设有一个amount下拉菜单,其元素为(1,2,3);

现在假设客户需要金额为 5 - 这是他的权利 - 它不存在于下拉列表中,因此客户现在必须以文本形式输入金额。因此,对于任何条目,客户必须从下拉列表中选择或以文本形式输入金额。

在描述了我的问题,以及我给你介绍的简单例子之后,这是我的问题:

是否有 HTML 代码可以使下拉菜单和文本字段合二为一,而不是分开?

【问题讨论】:

标签: html html-select html-input


【解决方案1】:

您可以使用 HTML5 <datalist> 原生地做到这一点:

<label>Choose a browser from this list:
<input list="browsers" name="myBrowser" /></label>
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Internet Explorer">
  <option value="Opera">
  <option value="Safari">
  <option value="Microsoft Edge">
</datalist>

【讨论】:

  • 它们的行为似乎因浏览器而异。在 Firefox (40.0.3) 中,我看到了建议“label1”和“label2”,而在 Chrome (45.0.2454.85 m) 中,我看到了建议“value1”和“value2”。它们的工作方式也略有不同,例如在他们对焦点/鼠标悬停的响应中,Chrome 按值显示标签。
  • 如果我删除 label= 属性,Firefox 会建议这些值,例如 Chrome。 (Chrome 永远不会建议标签 - 如果我删除 'value=' 属性,Chrome 不会显示任何内容。)
  • IE11 如果两者都存在则显示标签,如果仅存在标签则反直觉地不显示任何内容,如果存在值则显示值。因此,对于 IE、Firefox 和 Chrome,唯一一致的行为是,如果您只包含不带标签的值,您将获得一个值列表。
  • 我想知道是否某些兼容性问题与此特定示例中的选项标签不使用结束标签或结束斜杠有关?例如:&lt;option label='label1' value='value1' /&gt; 只是一个想法。
  • 我在 HTML5 实体中看到的另一个问题,接受的答案解决了(至少在我测试它的 Chrome 中)值的下拉列表过滤到仅与匹配的选项键入的文本。至少对于我的应用程序,即使输入了一些文本,我仍然希望能够看到完整的选项列表。我使用的是 HTML5 实体,但为了解决这个问题,我将切换 javascript 解决方案。
【解决方案2】:

选项 1

包含来自dhtmlgoodies 的脚本并像这样初始化:

<input type="text" name="myText" value="Norway"
       selectBoxOptions="Canada;Denmark;Finland;Germany;Mexico"> 
createEditableSelect(document.forms[0].myText);

选项 2

这是一个自定义解决方案,它结合了 &lt;select&gt; 元素和 &lt;input&gt; 元素,设置它们的样式,并通过 JavaScript 来回切换

<div style="position:relative;width:200px;height:25px;border:0;padding:0;margin:0;">
  <select style="position:absolute;top:0px;left:0px;width:200px; height:25px;line-height:20px;margin:0;padding:0;"
          onchange="document.getElementById('displayValue').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
    <option></option>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select>
  <input type="text" name="displayValue" id="displayValue" 
         placeholder="add/select a value" onfocus="this.select()"
         style="position:absolute;top:0px;left:0px;width:183px;width:180px\9;#width:180px;height:23px; height:21px\9;#height:18px;border:1px solid #556;"  >
  <input name="idValue" id="idValue" type="hidden">
</div>

【讨论】:

  • 固定高度和颜色会给你:jsfiddle.net/aljohnix/78mdmrs7
  • 谢谢,我使用了这个解决方案,并推荐了一些其他的调整。首先,在更改输入框时,将选择值重置为空字符串选项。否则,如果您选择一个选项然后对其进行编辑,则无法重置为该选项,因为它已在下拉列表中被选中,即使该值不再匹配。其次,这只是一个问题,它使我的页面上的其他样式看起来不错,突出了当前输入,焦点为选择集焦点到输入。下拉菜单仍然有效,但焦点突出显示正确。
【解决方案3】:

现代解决方案是“搜索”类型的输入字段!

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search https://www.w3schools.com/tags/tag_datalist.asp

在 HTML 中的某处定义数据列表以供以后参考:

<datalist id="mylist">
   <option value="Option 1">
   <option value="Option 2">
   <option value="Option 3">
</datalist>

然后您可以像这样定义您的搜索输入:

<input type="search" list="mylist">

瞧。非常好和容易。

【讨论】:

  • 好答案,但重要的不是输入字段类型。
  • @akauppi 是的,因为它属于“搜索”类型,因此您可以同时执行这两种操作 - 从列表中选择一个值或输入一个值,然后它将在列表中搜索。跨度>
  • “这些在功能上与文本输入相同,但用户代理的样式可能不同。” - 来自您的第一个链接。
  • @akauppi 我不确定“功能与文本输入相同”是什么意思。文本输入通常不会添加数据列表。
  • 这个答案似乎与 onaclov2000 的答案几乎相同 - 但有了这个答案,您可以使用搜索,如果没有“type='search'”规范,这似乎是不可能的。
【解决方案4】:

我发现这个问题和讨论非常有帮助,并想展示我最终得到的解决方案。它基于@DevangRathod 给出的答案,但我使用了 jQuery 并对它进行了一些调整,因此想展示一个完整的注释示例,以帮助其他从事类似工作的人。我最初一直在使用 HTML5 数据列表元素,但对该解决方案不满意,因为它从下拉列表中删除了与框中键入的文本不匹配的选项。在我的应用程序中,我希望完整列表始终可用。

完整功能演示在这里:https://jsfiddle.net/abru77mm/

HTML:

<!-- 
Most style elements I left to the CSS file, but some are here.
Reason being that I am actually calculating my width dynamically
in my application so when I dynamically formulate this HTML, I
want the width and all the factors (such as padding and border
width and margin) that go into determining the proper widths to
be controlled by one piece of code, so those pieces are done in
the in-line style.  Otherwise I leave the styling to the CSS file.
-->
<div class="data-list-input" style="width:190px;">
  <select class="data-list-input" style="width:190px;">
    <option value="">&lt;Free Form Text&gt;</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
    <!-- Note that though the select/option allows for a different
    display and internal value, there is no such distinction in the
    text box, so for all the options (except the "none" option) the
    value and content of the option should be identical. -->
  </select>
  <input class="data-list-input" style="width:160px;padding:4px 6px;border-width:1px;margin:0;" type="text" name="sex" required="required" value="">
</div>

JS:

jQuery(function() {
  //keep the focus on the input box so that the highlighting
  //I'm using doesn't give away the hidden select box to the user
  $('select.data-list-input').focus(function() {
    $(this).siblings('input.data-list-input').focus();
  });
  //when selecting from the select box, put the value in the input box
  $('select.data-list-input').change(function() {
    $(this).siblings('input.data-list-input').val($(this).val());
  });
  //When editing the input box, reset the select box setting to "free
  //form input". This is important to do so that you can reselect the
  //option you had selected if you want to.
  $('input.data-list-input').change(function() {
    $(this).siblings('select.data-list-input').val('');
  });
});

CSS:

div.data-list-input
{
    position: relative;
    height: 20px;
    display: inline-flex;
    padding: 5px 0 5px 0;
}
select.data-list-input
{
    position: absolute;
    top: 5px;
    left: 0px;
    height: 20px;
}
input.data-list-input
{
    position: absolute;
    top: 0px;
    left: 0px;
    height: 20px;
}

欢迎任何改进我的实施的 cmets。希望有人觉得这很有帮助。

【讨论】:

  • 如何限制用户输入?当用户点击文本框时,显示下拉值
  • @GANI 这并不是这种方法的重点。如果您不希望用户键入,则只需使用标准下拉列表本身。该解决方案的重点是当您希望允许用户输入他们自己的值时。
【解决方案5】:

您可以使用&lt;datalist&gt; 标签代替&lt;select&gt; 标签。

<input list="browsers" name="browser" id="browser">
<datalist id="browsers">
  <option value="Edge">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

【讨论】:

    【解决方案6】:

    我喜欢 jQuery Token 输入。实际上比上面提到的其他一些选项更喜欢 UI。

    http://loopj.com/jquery-tokeninput/

    另请参阅:http://railscasts.com/episodes/258-token-fields 了解说明

    【讨论】:

    • 这个插件是否支持任意(用户可选择)“令牌”?至少演示不建议:每当我输入预定义选项中不可用的内容时,当文本字段失去焦点时,我的文本会自动删除。
    【解决方案7】:

    受@ajdeguzman 的 js fiddle 启发(让我开心),这是我的节点/React 衍生产品:

     <div style={{position:"relative",width:"200px",height:"25px",border:0,
                  padding:0,margin:0}}>
        <select style={{position:"absolute",top:"0px",left:"0px",
                        width:"200px",height:"25px",lineHeight:"20px",
                        margin:0,padding:0}} onChange={this.onMenuSelect}>
                <option></option>
                <option value="starttime">Filter by Start Time</option>
                <option value="user"     >Filter by User</option>
                <option value="buildid"  >Filter by Build Id</option>
                <option value="invoker"  >Filter by Invoker</option>
        </select>
        <input name="displayValue" id="displayValue" 
               style={{position:"absolute",top:"2px",left:"3px",width:"180px",
                       height:"21px",border:"1px solid #A9A9A9"}}
               onfocus={this.select} type="text" onChange={this.onIdFilterChange}
               onMouseDown={this.onMouseDown} onMouseUp={this.onMouseUp} 
               placeholder="Filter by Build ID"/>
     </div>
    

    看起来像这样:

    【讨论】:

      【解决方案8】:

      我想添加一个基于 jQuery 自动完成的解决方案来完成这项工作。

      第 1 步:使列表固定高度且可滚动

      https://jqueryui.com/autocomplete/“可滚动”示例中获取代码,将最大高度设置为结果列表,以便它表现为选择框。

      第 2 步:打开焦点列表:

      Display jquery ui auto-complete list on focus event

      第 3 步:将最小字符数设置为 0,这样无论输入中有多少字符,它都会打开

      最终结果:

      <!doctype html>
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>jQuery UI Autocomplete - Scrollable results</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="/resources/demos/style.css">
        <style>
        .ui-autocomplete {
          max-height: 100px;
          overflow-y: auto;
          /* prevent horizontal scrollbar */
          overflow-x: hidden;
        }
        /* IE 6 doesn't support max-height
         * we use height instead, but this forces the menu to always be this tall
         */
        * html .ui-autocomplete {
          height: 100px;
        }
        </style>
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script>
        $( function() {
          var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
          ];
          $( "#tags" ).autocomplete({
      //      source: availableTags, // uncomment this and comment the following to have normal autocomplete behavior
            source: function (request, response) {
                response( availableTags);
            },
            minLength: 0
          }).focus(function(){
      //        $(this).data("uiAutocomplete").search($(this).val()); // uncomment this and comment the following to have autocomplete behavior when opening
              $(this).data("uiAutocomplete").search('');
          });
        } );
        </script>
      </head>
      <body>
      
      <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags">
      </div>
      
      
      </body>
      </html>
      

      在这里查看 jsfiddle:

      https://jsfiddle.net/bao7fhm3/1/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-25
        • 2022-12-05
        • 2011-01-02
        • 2018-08-09
        • 2016-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多