基于jquery实现多选下拉列表


Posted in jQuery onAugust 02, 2017

本文实例为大家分享了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">  
    <div class="width150">
      可多选年份:<input type="text" id="yearInput" placeholder="请选择年份" readonly>
      <ul id="yearId" class="hide">
      </ul>
    </div>
  </form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script>
  (function(){
    var str = '';
    var arr = {
      0 : {name:'2015',id:0},
      1 : {name:'2016',id:0},
      2 : {name:'2017',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>`;
    }
    $('#yearId').html(str);
  })();

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

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

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

效果图:

基于jquery实现多选下拉列表

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
QRCode.js:基于JQuery的生成二维码JS库的使用
Jun 23 jQuery
jQuery常用选择器详解
Jul 17 jQuery
jQuery绑定事件方法及区别(bind,click,on,live,one)
Aug 14 jQuery
jQuery DOM节点的遍历方法小结
Aug 15 jQuery
jquery在vue脚手架中的使用方式示例
Aug 29 jQuery
jQuery实现可兼容IE6的淡入淡出效果告警提示功能示例
Sep 20 jQuery
jquery.picsign图片标注组件实例详解
Feb 02 jQuery
详解jQuery-each()方法
Mar 13 jQuery
jquery分页优化操作实例分析
Aug 23 jQuery
jQuery/JS监听input输入框值变化实例
Oct 17 jQuery
jquery实现上传图片功能
Jun 29 jQuery
使用jquery实现轮播图效果
Jan 02 jQuery
jQuery接受后台传递的List的实例详解
Aug 02 #jQuery
基于Jquery Ajax type的4种类型(详解)
Aug 02 #jQuery
详解jquery选择器的原理
Aug 01 #jQuery
jQuery上传插件webupload使用方法
Aug 01 #jQuery
关于jquery form表单序列化的注意事项详解
Aug 01 #jQuery
简述jQuery Easyui一些用法
Aug 01 #jQuery
Jquery中.bind()、.live()、.delegate()和.on()之间的区别详解
Aug 01 #jQuery
You might like
Php部分常见问题总结
2006/10/09 PHP
PHP版 汉字转码的实现详解
2013/06/09 PHP
PHP+Ajax实现的检测用户名功能简单示例
2019/02/12 PHP
Javascript模块模式分析
2008/05/16 Javascript
Javascript 复制数组实现代码
2009/11/26 Javascript
基于jquery的loading 加载提示效果实现代码
2011/09/01 Javascript
详解AngularJS中的作用域
2015/06/17 Javascript
JavaScript 七大技巧(二)
2015/12/13 Javascript
字太多用...代替的方法(两种)
2017/03/15 Javascript
Node学习记录之cluster模块
2017/05/31 Javascript
深入理解vue-class-component源码阅读
2019/02/18 Javascript
零基础之Node.js搭建API服务器的详解
2019/03/08 Javascript
一文了解vue-router之hash模式和history模式
2019/05/31 Javascript
微信小程序框架的页面布局代码
2019/08/17 Javascript
Vue实现兄弟组件间的联动效果
2020/01/21 Javascript
vue-router懒加载的3种方式汇总
2021/02/28 Vue.js
[04:03]DOTA2肉山黑名单梦之声 风暴之灵中文配音鉴赏
2013/07/03 DOTA
python中使用正则表达式的后向搜索肯定模式(推荐)
2017/11/11 Python
python3 遍历删除特定后缀名文件的方法
2018/04/23 Python
python实现狄克斯特拉算法
2019/01/17 Python
Python中使用__new__实现单例模式并解析
2019/06/25 Python
北京RT科技有限公司.net工程师面试题
2013/02/15 面试题
应届毕业生自我评价分享
2013/12/15 职场文书
护理专科毕业生自荐书范文
2014/02/19 职场文书
车间机修工岗位职责
2014/02/28 职场文书
车队司机自我鉴定
2014/03/02 职场文书
优秀共产党员先进事迹材料
2014/05/06 职场文书
企业活动策划方案
2014/06/02 职场文书
公司任命书模板
2014/06/06 职场文书
英语分层教学实施方案
2014/06/15 职场文书
2014年社区民政工作总结
2014/12/02 职场文书
家长学校教学计划
2015/01/19 职场文书
客户答谢会致辞
2015/01/20 职场文书
《火烧云》教学反思
2016/02/23 职场文书
创业计划书之烤红薯
2019/09/26 职场文书
html5中sharedWorker实现多页面通信的示例代码
2021/05/07 Javascript