Simple sample code that simulates the select drop down box with jQuery

  • 2020-03-30 01:28:23
  • OfStack

Many times, artists find the default select drop-down box ugly (especially the drop-down arrow button on the right), and they often prefer to replace the button with a custom icon. This can only be used to simulate js + div, inverted, with jQuery simulation, of course, this kind of article on the Internet is not a few, just too lazy to look for


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title> Own implementation of the drop-down box </title>
    <style type="text/css" media="all">
        *{font-size:12px;line-height:18px;list-style:none;padding:0;margin:0;text-decoration:none;color:#000;border:0}
        .page{text-align:center;margin:50px;}
        input{border-bottom:solid 1px #ccc;height:18px}              
        .expand{display:none;position:absolute;width:200px;height:100px;overflow-y:auto;border:solid 1px #ccc};
  .expand li{margin:1px 0;background:#fff}
        .expand a{text-decoration:none;display:block;padding:0 5px;background:#efefef;margin:1px 0}
        .expand a:hover{background:#ff9}
    </style>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js"></script>
    <script type="text/javascript">
        function showExpand(targetId, expandId, expand_class) {
            //Close the other layers that pop up
            if (expand_class != undefined) {
                $("." + expand_class).hide();
            }
            //Determine if it is IE
            var isIE = (! +[1, ]);
            var expand = $("#" + expandId);
            var target = $("#" + targetId);
            var dx = 0;
            if (isIE) {
                dx = -2;
            }
            else {
                dx = 0;
            }
            expand.get(0).style.left = target.get(0).getBoundingClientRect().left + dx + "px";
            if (isIE) {
                dx = 17;
            }
            else {
                dx = 19;
            }
            expand.get(0).style.top = parseInt(target.get(0).getBoundingClientRect().top) + dx + "px";
            expand.show();
            //Assign a value when each li is clicked
            $("#" + expandId).find("li").each(function (i) {
                $(this).show().click(function () {
                    target.val($(this).text().split(' ')[1]);
                    expand.hide();
                })
            })
    
        }
  
        function search(srcId, expandId) {
            var expand = $("#" + expandId);
            var src = $("#" + srcId);
            var A = expand.find("a");
            var v = src.val().toUpperCase();
            A.each(function (i) {
                if (v.length >= 2) {
                    if ($(this).text().toUpperCase().indexOf(v) == -1) {
                        $(this).parent().hide();
                    }
                    else {
                        $(this).parent().show();
                    }
                }
                if (v.length <= 0) {
                    $(this).parent().show();
                }
            })
            src.val(v);
        }

  $().ready(function(){
   $("#txt_city").keyup(function(){
    search('txt_city','city_select1');
   }).focus(function(){
    showExpand('txt_city','city_select1','expand')
   })
   $("#txt_city2").keyup(function(){
    search('txt_city2','city_select2');
   }).focus(function(){
    showExpand('txt_city2','city_select2','expand')
   })
  })
  function fnTest(){
   document.getElementById("txtTarget").value = document.getElementById("txtSrc").value;
  }
    </script>
</head>
<body>
    <div class="page" style="text-align: center">
        <input type="text" value="" id="txt_city" class="input_expand"  /><a
            href="#" onclick="showExpand('txt_city','city_select1','expand')"> � </a>
        <div class="expand" id="city_select1">
            <ul>
                <li><a href="javascript:void(0)">SH  Shanghai </a></li>
                <li><a href="javascript:void(0)">BJ  Beijing </a></li>
                <li><a href="javascript:void(0)">HZ  hangzhou </a></li>
                <li><a href="javascript:void(0)">WH  wuhan </a></li>
                <li><a href="javascript:void(0)">NJ  nanjing </a></li>
                <li><a href="javascript:void(0)">WX  wuxi </a></li>
            </ul>
        </div>

        <input type="text" value="" id="txt_city2" class="input_expand"  /><a
            href="#" onclick="showExpand('txt_city2','city_select2','expand')"> � </a>
        <div class="expand" id="city_select2">
            <ul>
                <li><a href="javascript:void(0)">CN  Chinese </a></li>
                <li><a href="javascript:void(0)">EN  English </a></li>
                <li><a href="javascript:void(0)">JP  Japan </a></li>
                <li><a href="javascript:void(0)">RA  Russian </a></li>
                <li><a href="javascript:void(0)">FA  French </a></li>
                <li><a href="javascript:void(0)">00  other </a></li>
            </ul>
        </div>
  <br/>
  <br/>
  <input type="text" id="txtSrc" onkeyup="fnTest()" />
  <br/>
  <input type="text" id="txtTarget" />
    </div>
</body>
</html>

No picture, no truth, here's the truth:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201401/20140126102205.png ">
Disadvantages:
1, press the keyboard up and down keys, there is no highlighted automatic movement
2, when typing text automatic filtering results, it feels a bit unnatural relative to native select


Related articles: