ui組件之input多選下拉實(shí)現(xiàn)方法(帶有搜索功能)
我的風(fēng)格,先上效果圖,如果大家感覺還不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼。



廢話不說 先看div層次結(jié)構(gòu)
<!-- 最外層div 可以任意指定 主要用于定義子元素寬度 --> <div class="col-xs-10" style="width:800px"> <!-- 表單label 添加文字提示 --> <label for="" class="label-control">全文檢索</label> <!-- 多選承接div 以后會(huì)動(dòng)態(tài)添加span --> <div class="hint-input-span-container"> <!-- 表單元素 用來綁定監(jiān)聽事件以及接收用戶輸入 該層上方會(huì)動(dòng)態(tài)添加span --> <input type="text" name="hint-search" value="" placeholder="選定關(guān)鍵字或按下tab或按下enter來分割關(guān)鍵字"> </div> <!-- 包含下拉列表列 --> <div class="hint-block"> <!-- 根據(jù)json數(shù)據(jù)包動(dòng)態(tài)添加li --> <ul class="hint-ul"> </ul> </div> </div>
dom結(jié)構(gòu)注釋已經(jīng)能說得清楚了,下面來看css
* {
box-sizing: border-box;
}
.hint-input-span-container {
width:100%;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
display: inline-block;
padding: 2px 4px;
color: #555;
vertical-align: middle;
border-radius: 1px;
max-width: 100%;
line-height: 30px;
}
.hint-input-span-container .tag {
padding: -2px;
font-size: 12px;
font-family: serif;;
margin-right: 2px;
margin-top: 2px;
margin-bottom: 2px;
display: inline-block;
}
.label {
font-size: 10px;
padding: 4px 6px;
border: none;
text-shadow: none;
border-radius: 3px;
font-weight: 200;
}
.label-primary {
background: #2693FF;
color: white;
}
.hint-input-span-container span i[data-role='remove'] {
cursor: pointer;
}
.tag {
margin-right: 2px;
color: white;
}
.tag [data-role="remove"] {
margin-left: 2px;
cursor: pointer;
}
input[name='hint-search'] {
border: none;
box-shadow: none;
outline: none;
background-color: transparent;
padding: 0;
margin: 0;
width: 100%;
max-width: inherit;
}
.hint-block {
position: absolute;
width: 100px;
max-height: 120px;
background-color: #fff;
overflow: auto;
display: none;
z-index: 9999;
}
.hint-ul {
text-decoration: none;
list-style-type: none;
padding-left: 5px;
}
.hint-ul li{
font-size: 14px;
padding: 2px 4px;
}
.hint-ul li:hover{
background-color: #eee;
}
css中設(shè)置border-sizing:border-box很重要,這個(gè)屬性可以使padding與border計(jì)算在width之中
.hint-input-span-container 設(shè)置display為inline-block也很重要,有關(guān)于tag的排列
.hint-block設(shè)置z-index為9999保證顯示在最前端,同時(shí)position為absolute保證其位置
其他的都可以根據(jù)自己需要調(diào)整
下面來看js代碼
$(function(){
//json數(shù)據(jù)包
var data = {data:["123","北京你好","北京歡迎您","北京好","海洋","海洋廣利局","我海洋","我吃驚","我啦啦啦啦","我不能忍","機(jī)構(gòu)","日本","俄羅斯的山","埃塞俄比亞","伊巴卡","比比比"]};
//獲取后面需要多次調(diào)用的dom對(duì)象
var $hintSearch = $("input[name='hint-search']");
var $hintSearchContainer = $(".hint-input-span-container");
var $hintBlock = $(".hint-block");
var $hintUl = $(".hint-ul");
//初次調(diào)用添加詞典
addDictionary(data.data,addUlListener);
//設(shè)置詞典列表寬度
setHintSearchContainerWidth();
//實(shí)現(xiàn)響應(yīng)式 監(jiān)聽resize事件
$(window).bind('resize', setHintSearchContainerWidth);
//獲得焦點(diǎn)
$hintSearch.focus(function(){
animteDown();
});
//失去焦點(diǎn)
//設(shè)置延遲為了可以監(jiān)聽到click的響應(yīng)
$hintSearch.blur(function(){
setTimeout(function(){
animateUp();
},200);
});
//TAB 與 enter事件
//監(jiān)聽tab與enter兩個(gè)鍵位 如果input內(nèi)有輸入的內(nèi)容,則添加span
//注意最后要阻止一下事件冒泡 防止跳轉(zhuǎn)與切換焦點(diǎn)
$hintSearch.keydown(function(e){
switch (e.which) {
case 9: case 13:{
var text = $hintSearch.val();
if(!$.trim(text)) {
$hintSearch.val("");
e.preventDefault();
return;
}
if( !checkContainerHas(text) ) {
$hintSearch.before('<span class="tag label label-primary">'+ text +' <i class="fa fa-times" data-role="remove"></i><i> </i></span>');
addSpanListenr();
}
//console.log($hintSearch.val());
$hintSearch.val("");
$hintSearch.focus();
e.preventDefault();
break;
}
default: ;
}
});
//檢測(cè)輸入配對(duì)
//對(duì)輸入內(nèi)容在li中進(jìn)行匹配 如果包含字符串可以找到并返回
//搜索方法可以自行修改,只要保證返回一個(gè)搜索后的數(shù)組即可
$hintSearch.keyup(function(e){
var text = $hintSearch.val();
if (!$.trim(text)){
updateDictionary(data.data,addUlListener);
}
var tmparr = data.data.filter(function(x){
return x.indexOf(text) != -1;
})
if (tmparr.length === 0) {
tmparr.push("無匹配條目");
}
updateDictionary(tmparr,addUlListener);
})
//函數(shù)庫(kù)
//添加用戶常用字典庫(kù)
function addDictionary(dataarr, callback) {
for(var i = 0; i < dataarr.length; i++) {
$hintUl.append('<li>'+ dataarr[i] +'</li>');
}
callback();
}
//更新搜索內(nèi)容
function updateDictionary(dataarr,callback) {
$hintUl.empty();
addDictionary(dataarr,callback);
}
//向下滑動(dòng)動(dòng)畫
//封裝改變樣式邊框
function animteDown()
{
$hintBlock.slideDown('fast').css({'border':'1px solid #96C8DA','border-top' : '0px', 'box-shadow' : '0 2px 3px 0 rgba(34,36,38,.15)'});
$hintSearchContainer.css({'border':'1px solid #96C8DA','border-bottom' : '0px', 'box-shadow' : '0 2px 3px 0 rgba(34,36,38,.15)'});
}
//向上滑動(dòng)動(dòng)畫
function animateUp()
{
$hintBlock.slideUp('fast',function(){
$hintSearchContainer.css({'border':'1px solid #ccc'});
});
}
//檢驗(yàn)是否與輸入的重復(fù)
function checkContainerHas(text)
{
var flag = 0;
$(".hint-input-span-container span").each(function(){
if ($.trim(text) == $.trim($(this).text())) {
flag = 1;
return;
}
});
return flag ? true : false;
}
//設(shè)置hint-input-span-container寬度
function setHintSearchContainerWidth()
{
var hint_width = $hintSearchContainer.width() + 2 * parseInt($hintSearchContainer.css('padding-left').match(/[0-9]+/)[0]) + 2 * parseInt($hintSearchContainer.css('border-left').match(/[0-9]+/)[0]) ;
$hintBlock.css({'width': hint_width});
}
//綁定click事件
function addUlListener() {
$hintUl.delegate('li','click',function(){
var text = $(this).text();
if(!checkContainerHas(text)) {
$hintSearch.before('<span class="tag label label-primary">'+ text +' <i class="fa fa-times" data-role="remove"></i><i> </i></span>');
addSpanListenr();
}
$hintSearch.val("");
animateUp();
})
}
//監(jiān)聽 span事件
function addSpanListenr() {
$(".hint-input-span-container span").delegate("i",'click',function(){
$(this).parent().remove();
})
}
})
重點(diǎn)就是對(duì)事件的監(jiān)聽以及dom元素的操作,要依賴于jquery。
以上所述是小編給大家介紹的ui組件之input多選下拉實(shí)現(xiàn)方法(帶有搜索功能),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery UI實(shí)現(xiàn)動(dòng)畫效果代碼分享
這篇文章給大家總結(jié)了jQuery UI實(shí)現(xiàn)動(dòng)畫效果的實(shí)例代碼,有需要的朋友們可以參考測(cè)試下。2018-08-08
jQuery實(shí)現(xiàn)磁力圖片跟隨效果完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)磁力圖片跟隨效果,結(jié)合完整實(shí)例形式分析了jQuery事件響應(yīng)及animate方法實(shí)現(xiàn)帶緩沖效果的圖片跟隨效果,需要的朋友可以參考下2016-09-09
學(xué)習(xí)jQuery中的noConflict()用法
在本篇文章中我們整理了關(guān)于學(xué)習(xí)jQuery中的noConflict()用法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2018-09-09
jquery插件之信息彈出框showInfoDialog(成功/錯(cuò)誤/警告/通知/背景遮罩)
某某同學(xué)最近寫了個(gè)基于jquery的信息彈出插件showInfoDialog,該插件對(duì)背景進(jìn)行遮罩,然后彈出信息顯示框,信息顯示種類包括(操作成功/錯(cuò)誤信息/警告信息/通知信息)感興趣的朋友可以了解下2013-01-01
jQuery實(shí)現(xiàn)的tab標(biāo)簽切換效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的tab標(biāo)簽切換效果,結(jié)合實(shí)例形式分析了jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)變換頁(yè)面元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09
使用Javascript實(shí)現(xiàn)選擇下拉菜單互移并排序
本文給大家介紹使用js實(shí)現(xiàn)下拉菜單可選擇互相移動(dòng)并實(shí)現(xiàn)菜單排序,代碼簡(jiǎn)單易懂,具有參考價(jià)值,需要的朋友參考下吧2016-02-02
jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果(附源碼)
這篇文章主要介紹了jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果,以完整實(shí)例形式分析了jQuery結(jié)合jsp讀取MySQL數(shù)據(jù)庫(kù)操作實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果的相關(guān)技巧,并附帶完整實(shí)例源碼供讀者下載參考,需要的朋友可以參考下2015-12-12
jQuery實(shí)現(xiàn)frame之間互通的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)frame之間互通的方法,結(jié)合實(shí)例形式分析了jQuery實(shí)現(xiàn)frame父子框架之間的調(diào)用操作實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06

