Put input inside select

前端 未结 4 754
长情又很酷
长情又很酷 2020-12-12 00:55

Is there a way to place an INPUT field inside select?


                        
    
提交评论

  • 2020-12-12 01:13

    We can achive it putting the input tag "next to" and not "inside" the select tag but forcing them into a limited and fixed area like a table td.

    function selection(){
    	var selected=document.getElementById("select1").value;
      if(selected==0){
      	document.getElementById("input1").removeAttribute("hidden");
      }else{
      	//elsewhere actions
      }
    }
    td{
      width: 170px;
      max-width: 170px;
      overflow: hidden;
      white-space: nowrap;
    }
    
    input{
      width: 164px;
    }
    <table>
      <tr>
          <td>
            <input id="input1" hidden="hidden" placeholder="input data">
            <select onchange="selection()" id="select1">
              <option value="-1"></option>
              <option value="0">-make your own choice-</option>
              <option value="1">choice 1</option>
              <option value="2">choice 2</option>
            </select>
          </td>
      </tr>
    </table>

    jsfiddle

    0 讨论(0)
  • 2020-12-12 01:18

    <datalist> Element

    The datalist element is intended to provide a better mechanism for this concept.

    <input type="text" name="example" list="exampleList">
    <datalist id="exampleList">
      <option value="A">  
      <option value="B">
    </datalist>

    For more information, see

    • MDN HTML Reference - <datalist> Element
    0 讨论(0)
  • 2020-12-12 01:22

    It is not possible to place an INPUT field inside select. You can create your own custom plugin using jquery. Try the following code:

    HTML::

     <ul class="nav" role="navigation">
     <li class="dropdown"> <a href="#" id="drop2" role="button" class="dropdown-toggle" data-toggle="dropdown">--Select Country--<b class="caret"></b></a>
    
        <ul class="dropdown-menu" role="menu" aria-labelledby="drop2">
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">USA</a>
    
            </li>
            <li role="presentation" class="divider"></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">UK</a>
    
            </li>
            <li role="presentation" class="divider"></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Russia</a>
    
            </li>
            <li role="presentation" class="divider"></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Others <input type="text" id="other"/></a>
    
            </li>
        </ul>
    </li>
    </ul>
    

    CSS::

    .nav {
       margin-left: 0;
       margin-bottom: 20px;
       list-style: none;
      }
    
    ul, ol {
       padding: 0;
       margin: 0 0 10px 25px;
     }
    
    .dropup, .dropdown {
        position: relative;
     }
    
    .open>.dropdown-menu {
     display: block;
     }
    
    .nav>li>a {
     display: block;
     }
    
    .dropdown-menu {
     position: absolute;
     top: 100%;
     left: 0;
     z-index: 1000;
     display: none;
     float: left;
     min-width: 160px;
     padding: 5px 0;
     margin: 2px 0 0;
     list-style: none;
     background-color: #ffffff;
     border: 1px solid #ccc;
     border: 1px solid rgba(0, 0, 0, 0.2);
     -webkit-border-radius: 6px;
     -moz-border-radius: 6px;
      border-radius: 6px;
     -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
     -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
      box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
     -webkit-background-clip: padding-box;
     -moz-background-clip: padding;
      background-clip: padding-box;
      }
    
     .dropdown-menu .divider {
      height: 1px;
      margin: 9px 1px;
      overflow: hidden;
      background-color: #e5e5e5;
      border-bottom: 1px solid #ffffff;
      }
    
     .dropdown-menu>li>a {
      display: block;
      padding: 3px 20px;
      clear: both;
      font-weight: normal;
      line-height: 20px;
      color: #333333;
      white-space: nowrap;
     }
    

    Jquery::

      $(document).ready(function(){
     $('.dropdown-menu input').click(function (e) {
         e.stopPropagation();
     });
    
        $('.dropdown a').click(function(){
          $('.dropdown').addClass("open");
        });
    $('.dropdown-menu li').click(function(){
    
    $('.dropdown-toggle b').remove().appendTo($('.dropdown-toggle').text($(this).text()));
    });
    });
    

    Jsfiddle

    0 讨论(0)
  • 提交回复
    热议问题