帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» jQuery多选下拉框
jQuery多选下拉框
浏览:623次
-
评论:0次
-
发布时间:2017-07-04
Html
Css
Js
* { margin:0; padding:0; list-style:none; box-sizing:border-box; } .drop { width:300px; height:auto; margin:100px auto; } .drop-header { width:100%; padding:5px 10px; border:#c7c7c7 1px solid; word-wrap:break-word; min-height:32px; } .drop-header span { position:relative; display:inline-block; padding-right:5px; padding-left:5px; margin:1px 5px 1px; line-height:20px; background-color:#a9a9a9; color:#fff; cursor:pointer; } .drop-dwon { position:absolute; width:100%; height:auto; line-height:25px; border:#c7c7c7 1px solid; box-shadow:#c7c7c7 1px 1px 4px; max-height:200px; overflow-y:auto; overflow-x:hidden; } .drop-dwon li { padding:5px 10px; cursor:pointer; } .drop-dwon li:hover,.drop-dwon li.active:hover { color:#fff; background-color:#18717b; } .drop-dwon li.active { color:#fff; background-color:#07a4b5; }
$(function() { dropDwon({ id: 'drop', myData: { name1: { val: '111', selected: true, }, name2: { val: '222', }, name3: { val: '333' }, name4: { val: '444' }, name5: { val: '555' }, name6: { val: '666' }, name7: { val: '777' }, name8: { val: '888' }, } }) }) function dropDwon(option) { function drop(def) { this.name = def.id; this.init(); }; drop.prototype = { init: function() { this.list(); this.myClick(); this.callBack(); }, //添加下拉框到页面中 list: function() { var id = '#' + this.name; var header = '
'; //默认下拉框选项 $(id).append(header); if (def.type == 'single') { for (var key in def.myData) { if (def.myData[key].selected == true) { var headerTxt = def.myData[key].val; $(id).find('.drop-header').text(headerTxt) }; }; }; var newPo = $(id).offset(); var newW = parseInt($(id).css('width')); var newH = parseInt($(id).css('height')); $('.drop-dwon').css({ width: newW + 'px', top: newPo.top + newH + 'px', left: newPo.left + 'px' }); }, //下拉框点击事件 myClick: function() { var self = this; var name = this.name; var id = '#' + this.name; $(id).click(function(e) { e.stopPropagation(); $('.drop-dwon').remove(); var _this = $(this); var lists, drop; lists = ''; for (var key in def.myData) { lists += '
' + def.myData[key].val + '
'; }; var drop = '
' + lists + '
'; $('body').append(drop); self.position('.drop-dwon'); //改变屏幕宽度的时候,重新计算下拉框内容的位置 window.onresize = function() { self.position('.drop-dwon'); }; if (def.type == 'sigle') { self.sinClick(); } else if (def.type == 'multi') { self.mltClick(); } }); }, //计算下拉框内容的位置 position: function(obj) { var id = '#' + this.name; var myPo = $(id).offset(); var myW = parseInt($(id).css('width')); var myH = parseInt($(id).css('height')) - 1; $(obj).css({ top: myPo.top + myH + 'px', left: myPo.left + 'px', width: myW + 'px', }); }, sinClick: function() { var name = this.name; var id = '#' + name + 'con'; $(id).on('click', 'li', function(e) { e.stopPropagation(); $('#' + name).find('.drop-header').text($(this).text()) }); $(document).click(function() { $(id).remove(); }); }, mltClick: function() { var self = this; var name = this.name; var id = '#' + name + 'con'; var header = $('#' + name).find('.drop-header'); $(id).on('click', 'li', function(e) { e.stopPropagation(); var sel = $(this).attr('sel'); if (sel == 'true') { $(this).removeClass('active'); $(this).attr('sel', false); var rem = $(this).attr('value'); header.find('span').each(function() { if ($(this).attr('vel') == rem) { $(this).remove(); }; }); } else { $(this).addClass('active'); $(this).attr('sel', true); var txt = '
' + $(this).text() + '
'; header.append(txt); }; self.position('.drop-dwon'); self.mtlRemove(); }); $(document).click(function() { $(id).remove(); }); }, mtlRemove: function() { var self = this; var name = this.name; var id = '#' + name; $(id + ' ' + 'span').click(function(e) { e.stopPropagation(); var _this = $(this); var vle = _this.attr('vel'); _this.remove(); $(id + 'con li').each(function() { var vleLi = $(this).attr('value'); if (vleLi == vle) { $(this).removeClass('active'); $(this).attr('sel', false); }; }); self.position('.drop-dwon'); }); }, callBack: function() { def.callBack(); } }; var def = { type: 'multi', //multi:多选;sigle:单选 myData: { name1: { val: '默认选中', selected: true, }, name2: { val: '下拉1', }, name3: { val: '下拉2' }, name4: { val: '下拉3' }, }, }; def = $.extend(def, option); new drop(def); }
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.10.2
<!doctype html><html><head><meta charset="utf-8"><title>jQuery多选下拉框-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:简单单选下拉框,多选下拉框
获取所需日期的计算方法
jQuery轮播图
讨论这个常用代码(0)
回答他人问题或分享心得会奖励牛币
〒_〒 居然一个评论都没有……
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
623
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值