用jQuery實(shí)現(xiàn)可輸入多選下拉組合框?qū)嵗a
【寫在前面的話】網(wǎng)站上很多用各種插件,比如依賴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è)可填寫的下拉框有點(diǎn)得不償失。
基于這種原因,于是私下用jquery寫了一個(gè)比較簡(jiǎn)單的多選下拉可填寫組合框?! ?/p>
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">最愛(à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">蘋果</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的多選下拉框(自寫)
- 利用Jquery實(shí)現(xiàn)可多選的下拉框
- Jquery多選下拉列表插件jquery multiselect功能介紹及使用
- jquery multiSelect 多選下拉框
- jQuery EasyUI API 中文文檔 - ComboBox組合框
相關(guān)文章
底部懸浮通欄可以關(guān)閉廣告位的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇底部懸浮通欄可以關(guān)閉廣告位的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06jQuery Raty星級(jí)評(píng)分插件使用方法實(shí)例分析
這篇文章主要介紹了jQuery Raty星級(jí)評(píng)分插件使用方法,結(jié)合實(shí)例形式分析了jquery Raty星級(jí)評(píng)分插件的下載、調(diào)用、基本屬性及相關(guān)使用技巧,需要的朋友可以參考下2019-11-11jquery實(shí)現(xiàn)頂部向右伸縮的導(dǎo)航區(qū)域代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)頂部向右伸縮的導(dǎo)航區(qū)域代碼,涉及jquery鼠標(biāo)click點(diǎn)擊事件及頁(yè)面元素動(dòng)態(tài)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09jQuery 鼠標(biāo)經(jīng)過(guò)(hover)事件的延時(shí)處理示例
一般情況下,我們是不對(duì)鼠標(biāo)hover事件進(jìn)行延時(shí)處理。但是,有時(shí)候,為了避免不必要的干擾,常會(huì)對(duì)鼠標(biāo)hover事件進(jìn)行延時(shí)處理2014-04-04jQuery拖動(dòng)div、移動(dòng)div、彈出層實(shí)現(xiàn)原理及示例
正如標(biāo)題所言的實(shí)現(xiàn)原理是使div的position為絕對(duì)定位absolute,然后控制其top與left值,需要的朋友可以參考下2014-04-04jQuery調(diào)用RESTful WCF示例代碼(GET方法/POST方法)
本篇文章主要介紹了jQuery調(diào)用RESTful WCF示例代碼(GET方法/POST方法),需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01