现在位置: 首页 > 博客文章 > 电脑相关 > IT开发 > 开发语言 > Web > 正文
html中可以自定义输入的select下拉列表
2016年11月24日 10:22:47 Web ⁄ 共 1774字 暂无评论 ⁄ 被围观 2,505次

在项目开发中,往往有这种需求:那就是需要下拉选择已有的数据列表,当没有自己需要的数据时,往往需要去管理这些列表数据的画面去添加,或者在下拉列表后面放一个快捷按钮,先添加,然后再选。

那么问题就来了,如果按照上面的操作,往往需要很多步骤,能不能当没有可选项目时,直接在下拉列表上输入呢?

答案是可以的。下面就是用 JS 实现了下拉列表上自定义选项的功能,请参考。

Code   ViewPrint
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>可编辑的下拉列表</title>
  5.     </head>
  6.     <style type="text/css">
  7.         .cls_select_span {
  8.             position:absolute;
  9.             border:1pt solid #c1c1c1;
  10.             overflow:hidden;
  11.             width:188px;
  12.             height:19px;
  13.             clip:rect(-1px 190px 190px 170px);
  14.         }
  15.         .cls_input_span {
  16.             position:absolute;
  17.             border-top:1pt solid #c1c1c1;
  18.             border-left:1pt solid #c1c1c1;
  19.             border-bottom:1pt solid #c1c1c1;
  20.             width:170px;
  21.             height:19px;
  22.         }
  23.         .cls_option_defined {
  24.             color:#c2c2c2;
  25.         }
  26.         .cls_select {
  27.             width:190px;
  28.             height:20px;
  29.             margin:-2px;
  30.         }
  31.         .cls_input {
  32.             width:170px;
  33.             height:15px;
  34.             border:0pt;
  35.         }
  36.     </style>
  37.     <body>
  38.     <span class="cls_select_span">
  39.         <select id="id_select" class="cls_select" onChange="selectOnChangeFunc()">
  40.             <option value="" class="cls_option_defined">---自定义---</option>
  41.             <option value="开发部">开发部</option>
  42.             <option value="市场部">市场部</option>
  43.             <option value="销售部">销售部</option>
  44.         </select>
  45.     </span>
  46.     <span class="cls_input_span">
  47.         <input type="text" id="id_input" class="cls_input" value="可选择也可自定义输入">
  48.     </span>
  49.     <script>
  50.         function selectOnChangeFunc() {
  51.             document.getElementById('id_input').value = document.getElementById('id_select').options[document.getElementById('id_select').selectedIndex].value;
  52.         }
  53.     </script>
  54.     </body>
  55. </html>

Good Luck!

给我留言

留言无头像?