首頁 > web前端 > js教程 > 主體

使用jquery實作下拉清單選項

巴扎黑
發布: 2018-05-14 15:01:44
原創
1641 人瀏覽過

這篇文章主要為大家詳細介紹了基於jquery實現多選下拉列表,具有一定的參考價值,有興趣的小伙伴們可以參考一下

本文實例為大家分享了jquery實現多選取下拉清單展示的具體程式碼,供大家參考,具體內容如下

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
   ul li{
    list-style: none;
    }
   .hide{display: none}
   .width150{
    width: 150px;
    }
   .width150 input[type="text"]{
     width: 100%; 
     height: 32px; 
     border: 1px solid #ccc; 
     border-radius: 4px; 
     padding-left: 12px;
   }
    .width150 ul{ 
      width: 96%; 
      padding: 0 0 0 20px; 
      margin: 0; 
      border: 1px solid #ccc; 
    }
    .width150 li{ 
      padding: 5px; 
    }
    .width150 li+li{ 
      border-top: 1px solid #ccc; 
    }
  </style>
</head>
<body> 
  <form id="form">  
    <p class="width150">
      可多选年份:<input type="text" id="yearInput" placeholder="请选择年份" readonly>
      <ul id="yearId" class="hide">
      </ul>
    </p>
  </form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script>
  (function(){
    var str = &#39;&#39;;
    var arr = {
      0 : {name:&#39;2015&#39;,id:0},
      1 : {name:&#39;2016&#39;,id:0},
      2 : {name:&#39;2017&#39;,id:0}
    };
    for (let x in arr){
      console.info(x);
      str += `<li><label><input type="checkbox" value="${arr[x].id}" data-name="${arr[x].name}">${arr[x].name}</label></li>`;
    }
    $(&#39;#yearId&#39;).html(str);
  })();

  $("#yearInput").click(function(){
    $(this).attr(&#39;placeholder&#39;,&#39;&#39;);
    var name = &#39;&#39;;
    $(&#39;#yearId input&#39;).each(function () {//循环遍历checkbox
      var check=$(this).is(&#39;:checked&#39;);//判断是否选中
      if(check){
        name += $(this).attr(&#39;data-name&#39;)+&#39;,&#39;;
        $(this).attr(&#39;name&#39;,"yearId");
      }else {
        $(this).attr(&#39;name&#39;,"");
      }
    });
    $("#yearInput").val(name.slice(0,-1));//去除最后的逗号
  });

  $("#yearId").mouseover(function() {
    if (!$("#yearId").hasClass(&#39;hover&#39;)){//类hover在下面用来验证是否需要隐藏下拉,没有其他用途。
      $("#yearId").addClass(&#39;hover&#39;);
    }
  }).mouseout(function(){
    $("#yearId").removeClass(&#39;hover&#39;);
  });

  $(document).on(&#39;click&#39;,function() {
    if (!$("#yearInput").is(":focus") && !$("#yearId").hasClass(&#39;hover&#39;)) {//如果没有选中输入框且下拉不在hover状态。
      var name = &#39;&#39;;
      $(&#39;#yearId input&#39;).each(function () {//遍历checkbox
        var check = $(this).is(&#39;:checked&#39;);//判断是否选中
        if (check) {
          name += $(this).attr(&#39;data-name&#39;) + &#39;,&#39;;
          $(this).attr(&#39;name&#39;, "yearId");
        } else {
          $(this).attr(&#39;name&#39;, "");
        }
      });
      $("#yearInput").val(name.slice(0, -1));//去除最后的逗号
      $("#yearId").addClass(&#39;hide&#39;);
    } else {
      $("#yearId").removeClass(&#39;hide&#39;);
    }
  });
</script>
</html>
登入後複製

效果圖:

以上是使用jquery實作下拉清單選項的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!