Rozwijane Menu / pole tekstowe w jednym

Pracuję nad budową nowej witryny i potrzebuję rozwijanego menu, aby wybrać ilość czegoś w mojej witrynie. Ale jednocześnie potrzebuję tej rozwijanej listy, aby zaakceptować tekst. Więc jeśli klient chce wybrać z rozwijanej listy, to może, również jeśli klient chce wprowadzić kwotę za pomocą tekstu, to może również. Jak widzisz, chcę zrobić podwójną.

Na przykład: załóżmy, że istnieje amount rozwijane menu, a jego elementami są (1,2,3);

Załóżmy teraz, że klient potrzebuje kwoty 5 - to jest jego prawo - nie istnieje na rozwijanej liście, więc klient musi teraz wpisać kwotę tekstowo. Tak więc dla każdego wpisu klient musi wybrać z rozwijanej listy lub wpisać kwotę tekstowo.

Po opisie mojego problemu i prostym przykładzie, który wam przedstawiłem, oto moje pytanie:

Czy istnieje kod HTML do tworzenia rozwijanego menu i pola tekstowego w jednym, jako podwójne, Nie rozdzielone?

Author: KyleMit, 2013-08-19

6 answers

Opcja 1

Dołącz skrypt z dhtmlgoodies i zainicjalizuj w następujący sposób:

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

Opcja 2

Oto niestandardowe rozwiązanie, które łączy element <select> i element <input>, stylizuje je i przełącza się tam iz powrotem za pomocą 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>
 32
Author: Devang Rathod,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-04 15:20:02

Możesz to zrobić natywnie z 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>
 59
Author: onaclov2000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-04 15:29:08

Uznałem to pytanie i dyskusję za bardzo pomocne i chciałem pokazać rozwiązanie, z którym skończyłem. Jest on oparty na odpowiedzi udzielonej przez @ DevangRathod, ale użyłem jQuery i zrobiłem kilka poprawek do niego, więc chciałem pokazać w pełni skomentowaną próbkę, aby pomóc każdemu, kto pracuje nad czymś podobnym. Początkowo używałem elementu listy danych HTML5, ale był niezadowolony z tego rozwiązania, ponieważ usuwa opcje z rozwijanej listy, które nie pasują do tekstu wpisanego w polu. W moim aplikacja, chciałem, aby pełna lista była zawsze dostępna.

W pełni funkcjonalne demo tutaj: 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;
}

Wszelkie uwagi do poprawy mojej realizacji mile widziane. Mam nadzieję, że ktoś uzna to za pomocne.

 3
Author: Daniel Skarbek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-05 05:03:29

Lubię jQuery Token input. Właściwie wolę interfejs użytkownika niż niektóre z innych opcji wymienionych powyżej.

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

Zobacz też: http://railscasts.com/episodes/258-token-fields dla wyjaśnienia

 1
Author: Matthias,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-08 21:22:47

Zainspirowany js fiddle by @ ajdeguzman (made my day), oto moja pochodna node/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>

WyglÄ…da tak:

Tutaj wpisz opis obrazka

 0
Author: Gunnar Forsgren - Mobimation,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-05 14:36:35

Chciałbym dodać rozwiązanie oparte na jQuery autocomplete, które wykonuje zadanie.

Krok 1: Ustaw stałą wysokość i przewijanie listy

Pobierz kod z https://jqueryui.com/autocomplete / przykład "Scrollable", ustawiający maksymalną wysokość na liście wyników tak, aby zachowywała się jak pole wyboru.

Krok 2: otwórz Listę na ostrości:

Wyświetlanie listy jQuery UI auto-complete na zdarzeniu focus

Krok 3: Ustaw Minimalne znaki na 0, aby otwierały się bez względu na liczbę znaki są na wejściu

Wynik końcowy:

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

Sprawdź jsfiddle tutaj:

Https://jsfiddle.net/bao7fhm3/1/

 0
Author: Alexandru Trandafir Catalin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-17 15:55:00