用jQuery實(shí)現(xiàn)可輸入多選下拉組合框?qū)嵗a
【寫(xiě)在前面的話】網(wǎng)站上很多用各種插件,比如依賴(lài)bootstrap的bootstrap-select插件等。雖然這些框架可以實(shí)現(xiàn)很多功能,但因?yàn)樵趯?shí)際項(xiàng)目中,可能只會(huì)用到其中的某個(gè)功能,若是一概引入,會(huì)導(dǎo)致整個(gè)js加載過(guò)于笨重。比如前面提到的bootstrap-select插件,在不壓縮的情況下,達(dá)到300多k。因此,為了實(shí)現(xiàn)一個(gè)可填寫(xiě)的下拉框有點(diǎn)得不償失。
基于這種原因,于是私下用jquery寫(xiě)了一個(gè)比較簡(jiǎn)單的多選下拉可填寫(xiě)組合框。
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">最?lèi)?ài)的水果</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">蘋(píng)果</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')
});
由于本組件中使用了數(shù)組的map方法,可能此方法在ie中不能兼容。由于鄙人電腦ie無(wú)法打開(kāi),用360瀏覽器測(cè)試后同樣可是正常使用。
- jQuery實(shí)現(xiàn)下拉框多選 jquery-multiselect 的實(shí)例代碼
- 基于jQuery下拉選擇框插件支持單選多選功能代碼
- jQuery使用Selectator插件實(shí)現(xiàn)多選下拉列表過(guò)濾框(附源碼下載)
- 自定義Angular指令與jQuery實(shí)現(xiàn)的Bootstrap風(fēng)格數(shù)據(jù)雙向綁定的單選與多選下拉框
- 一個(gè)簡(jiǎn)單的jquery的多選下拉框(自寫(xiě))
- 利用Jquery實(shí)現(xiàn)可多選的下拉框
- Jquery多選下拉列表插件jquery multiselect功能介紹及使用
- jquery multiSelect 多選下拉框
- jQuery EasyUI API 中文文檔 - ComboBox組合框
相關(guān)文章
jQuery中select與datalist制作下拉菜單時(shí)的區(qū)別淺析
一般我們通常使用select制作下拉菜單,但是H5之后,datalist也可以充當(dāng)select的角色,而且兩者還有一點(diǎn)小的不同。具體區(qū)別詳解小編通過(guò)本文給簡(jiǎn)單介紹下2016-12-12
jQuery插件pagination實(shí)現(xiàn)無(wú)刷新分頁(yè)
本文我們一步一步看看使用jQuery、JSON、Ajax和微軟jQuery Template插件組合實(shí)現(xiàn)jQuery無(wú)刷新分頁(yè),希望能給你一些幫助,感興趣的朋友可以參考一下2016-05-05
jquery實(shí)現(xiàn)的縮略圖預(yù)覽滑塊實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)的縮略圖預(yù)覽滑塊,實(shí)例分析了jQuery縮略圖預(yù)覽滑塊的實(shí)現(xiàn)與使用技巧,并提供了完整的實(shí)例下載,需要的朋友可以參考下2015-06-06
jQuery動(dòng)態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法
這篇文章主要介紹了jQuery動(dòng)態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法,涉及jQuery操作json結(jié)構(gòu)數(shù)據(jù)及鼠標(biāo)事件的技巧,需要的朋友可以參考下2015-05-05
基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化
這篇文章主要為大家詳細(xì)介紹了基于jQuery的時(shí)間戳與日期間的轉(zhuǎn)化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
這篇文章主要介紹了jquery序列化form表單,使用ajax提交后處理返回的json數(shù)據(jù)的示例,需要的朋友可以參考下2014-03-03
hovertree插件實(shí)現(xiàn)二級(jí)樹(shù)形菜單(簡(jiǎn)單實(shí)用)
hovertree是一個(gè)仿京東的樹(shù)形菜單jquery插件,暫時(shí)有銀色和綠色兩種。本文將對(duì)此進(jìn)行介紹。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12

