【写在前面的话】网站上很多用各种插件,比如依赖bootstrap的bootstrap-select插件等。虽然这些框架可以实现很多功能,但因为在实际项目中,可能只会用到其中的某个功能,若是一概引入,会导致整个js加载过于笨重。比如前面提到的bootstrap-select插件,在不压缩的情况下,达到300多k。因此,为了实现一个可填写的下拉框有点得不偿失。
基于这种原因,于是私下用jquery写了一个比较简单的多选下拉可填写组合框。
container{
margin: 20px auto;
padding:0 15px;
width: 50%;
height:300px;
box-sizing: border-box;
}
.text-container{
display: inline-block;
float:left;
width: 15%;
height: 32px;
line-height: 32px;
box-sizing: border-box;
}
.selectContainer{
width: 70%;
height:200px;
float:left;
position: relative;
padding:0;
margin:0;
box-sizing: border-box;
}
.selectedContent{
width:85%;
height: 25px;
float:left;
}
.dropDown-toggle{
width:14%;
height:31px;
line-height: 31px;
text-align: center;
border: 1px solid silver;
border-left:none;
float:left;
padding:0;
margin:0;
box-sizing: border-box;
cursor: pointer;
}
.dropDown-menu{
margin:0;
padding:0 15px 10px;
width:100%;
border:1px solid silver;
border-top: none;
box-sizing: border-box;
list-style: none;
position: absolute;
top:31px;
right:0;
}
.items{
margin-top:8px;
padding: 2px;
cursor: pointer;
}
.items:hover{
background: #ddd;
}
.isSelectedText{
display: inline-block;
width:90%;
}
.dsn{
display: none;
}
<div class="container">
<span class="text-container">最爱的水果</span>
<div class="multipleSelect selectContainer">
<input type="text" class="selectedContent">
<div class="dropDown-toggle">选择</div>
<ul class="dropDown-menu dsn">
<li class="items">
<span class="isSelectedText">苹果</span>
<span class="isSelected"><input type="checkbox"></span>
</li>
<li class="items">
<span class="isSelectedText">梨</span>
<span class="isSelected"><input type="checkbox"></span>
</li>
<li class="items">
<span class="isSelectedText">橘子</span>
<span class="isSelected"><input type="checkbox"></span>
</li>
<li style="text-align: right">
<button type="button" class="confirmSelect">确定</button>
</li>
</ul>
</div>
</div>
$('.isSelected input[type=checkbox]').on('click', function(){
var selectedItems = $(this).parents('.dropDown-menu').prevAll('.selectedContent').val().split(' ');
var thisItem = $(this).parent().prev().text();
var isExisted = 0;
var isChecked = $(this).is(':checked');
if(isChecked){
selectedItems.map(function(item, index){
if(item === thisItem){
isExisted++
}
});
if(!isExisted){
selectedItems.push(thisItem)
}
}
else{
selectedItems.map(function(item, index){
if(item === thisItem){
selectedItems.splice(index, 1);
}
});
}
$(this).parents('.dropDown-menu').prevAll('.selectedContent').val(selectedItems.join(' '));
})
$('.confirmSelect').on('click', function(){
$(this).parents('.dropDown-menu').addClass('dsn');
})
$('.dropDown-toggle').on('click', function(){
$(this).next().toggleClass('dsn')
});
由于本组件中使用了数组的map方法,可能此方法在ie中不能兼容。由于鄙人电脑ie无法打开,用360浏览器测试后同样可是正常使用。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“用jQuery实现可输入多选下拉组合框实例代码”评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]