jQuery+vue.js實(shí)現(xiàn)的多選下拉列表功能示例
本文實(shí)例講述了jQuery+vue.js實(shí)現(xiàn)的多選下拉列表功能。分享給大家供大家參考,具體如下:
其實(shí)就是實(shí)現(xiàn)一個多選下拉列表,然后將選中的選項(xiàng)顯示到相應(yīng)的位置;
因?yàn)橹饕莏Query選中行為的實(shí)現(xiàn),所以,樣式結(jié)構(gòu)就不多說啦,直接貼代碼啦:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/vue/1.0.14/vue.js"></script>
<title>多選下拉</title>
</head>
<body>
<div class="zj-div">
<div class="btn">全部級別</div>
<ul>
<li class='list' v-for="item in myData">{{item}}</li>
</ul>
</div>
</body>
</html>
li表單我這里利用了vue進(jìn)行了簡單的的雙向數(shù)據(jù)綁定,哈哈哈 也是很偷懶啦
*{
padding: 0px;
margin: 0px;
}
.zj-div{
position: relative;
left: 50px;
top: 50px;
}
.btn,li{
width: 200px;
height: 50px;
border: 1px solid #01bfda;
border-radius: 15px;
background: #000d16;
color:white;
line-height: 50px;
text-align: center;
font-size: 18px;
}
ul {
display: none;
width: 220px;
}
li {
list-style: none;
}
li:hover{
cursor: pointer;
background: #535a5c;
}
li[check="true"] {
background: #01bfda;
}
有一點(diǎn)需要注意的是,因?yàn)橐獙?shí)現(xiàn)多選,我想的是,選中的項(xiàng)與未選中的項(xiàng)通過不同的背景顏色進(jìn)行區(qū)分;
所以就綁定了check屬性,當(dāng)check='true'時(shí),背景顏色不同;
下面就是重點(diǎn)啦,畫圈圈~~~
真的完全是利用自己的“強(qiáng)大”邏輯思維實(shí)現(xiàn)的,哈哈哈,也是很冗余啦~
因?yàn)椴幌胫苯右媒M件,所以心血來潮就自己動手了,代碼中估計(jì)都能看出我的思考過程了吧~~~~
可以說是很費(fèi)勁了,奈何因?yàn)榉椒ú皇煜ぜ由喜惶私馊绾蝺?yōu)化,用的最笨的方法-----根據(jù)最后要達(dá)到的目標(biāo),考慮會出現(xiàn)的情況,完成的最初的版本但也是最好理解的版本(雖然我都嫌棄有點(diǎn)長):
new Vue({
el:".zj-div",
data:{
myData:["全部級別","一級","二級","三級"],
}
})
$(document).ready(function(){
var len = $('ul').children('li').length;
$('.btn').click(function(e) {
$('ul').slideToggle();
e.stopPropagation();
}); //點(diǎn)擊.btn實(shí)現(xiàn)ul的收放
$(document).not($('.list')).click(function(e){
$('ul').slideUp();
}) //.not方法就是除去當(dāng)前這個元素
//點(diǎn)擊頁面除了li的其他部分時(shí),ul收起
for(let i = 0; i < len; i++){
var firstAll = $('ul').children().first();
var arr = []; //為綁定.btn的值創(chuàng)建一個數(shù)組
$('li').eq(i).click(function(e){
e.stopPropagation(); //因?yàn)槭录芭輽C(jī)制,一定要注意取消時(shí)間冒泡
if($(this).attr('check')!=="true"){
if($(this).text()=="全部級別"){ //如果當(dāng)前點(diǎn)擊的是“全部級別”,則所有的li背景都改變
$(this).attr('check','true');
$(this).siblings().attr('check',"true");
// arr.push($(this).text());
$('.btn').text($(this).text());
arr = ["一級","二級","三級"];
//此時(shí).btn顯示"全部級別"
}else{
$(this).attr('check','true'); //如果當(dāng)前點(diǎn)擊的li是其他的,則當(dāng)前l(fā)i背景改變
if(arr.includes($(this).text())){
$('.btn').text(arr); //分情況討論此時(shí).btn應(yīng)該如何顯示
}else{ //注意結(jié)合arr
arr.push($(this).text());
$('.btn').text(arr);
}
}
if($(this).text()!=="全部級別"&&firstAll.next().attr('check')=='true'&&firstAll.next().next().attr('check')=='true'&&firstAll.next().next().next().attr('check')=='true'){
$('ul').children().first().attr('check','true');
$('.btn').text($('ul').children().first().text());
} //if判斷語句,我覺得肯定有其他的方法,我這個簡直太簡單粗暴了,可是我還沒想到...
//這是我們應(yīng)該考慮的一種情況,當(dāng)其他幾項(xiàng)全選時(shí),"全部級別"應(yīng)該默認(rèn)被選中
}else{
if($(this).text()=="全部級別"){ //同理,當(dāng)當(dāng)前元素被選中,再被點(diǎn)擊時(shí)要取消選中
$(this).attr('check','false');
$(this).siblings().attr('check',"false");
$('.btn').text($(this).text()); //注意此時(shí),雖然.btn顯示為"全部級別"
arr = []; //但實(shí)際上沒有任何元素被選中,所以arr實(shí)際為空
}else{
$(this).attr('check','false');
$('ul').children().first().attr('check','false');
for(var a = 0 ; a < arr.length; a++){
if(arr[a] == $(this).text()){
arr.splice(a,1); //數(shù)組方法,刪除索引為a的一個元素
$('.btn').text(arr);
if(arr.length == 0){ //如果arr數(shù)據(jù)為空,那么.btn顯示"全部級別"
$('.btn').text(firstAll.text())
}
}
}
}
}
})
}
})
見解也就添加到注釋里面啦~~哈哈哈 反正也是自己看 吼吼吼~~~
好啦 效果圖:

慢慢的學(xué)習(xí)下來,我算是真的發(fā)現(xiàn),好多東西,在真正動手前總覺得好像蠻簡單,可一旦入坑,就會陷入長久的困惑......
去做的過程中,總會發(fā)現(xiàn)新的問題~~~所以 我就記一下,免得下次又有同樣的需求,我又要重新思考 哈哈哈哈 也是很偷懶啦~~~畢竟 嗯 記憶力太差
That`s all~~
Happy Ending!!!
這里再給出一個完整示例代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/vue/1.0.14/vue.js"></script>
<title>www.fzitv.net 多選下拉</title>
<style>
*{
padding: 0px;
margin: 0px;
}
.zj-div{
position: relative;
left: 50px;
top: 50px;
}
.btn,li{
width: 200px;
height: 50px;
border: 1px solid #01bfda;
border-radius: 15px;
background: #000d16;
color:white;
line-height: 50px;
text-align: center;
font-size: 18px;
}
ul {
display: none;
width: 220px;
}
li {
list-style: none;
}
li:hover{
cursor: pointer;
background: #535a5c;
}
li[check="true"] {
background: #01bfda;
}
</style>
</head>
<body>
<div class="zj-div">
<div class="btn">全部級別</div>
<ul>
<li class='list' v-for="item in myData">{{item}}</li>
</ul>
</div>
<script>
new Vue({
el:".zj-div",
data:{
myData:["全部級別","一級","二級","三級"],
}
})
$(document).ready(function(){
var len = $('ul').children('li').length;
$('.btn').click(function(e) {
$('ul').slideToggle();
e.stopPropagation();
}); //點(diǎn)擊.btn實(shí)現(xiàn)ul的收放
$(document).not($('.list')).click(function(e){
$('ul').slideUp();
}) //.not方法就是除去當(dāng)前這個元素
//點(diǎn)擊頁面除了li的其他部分時(shí),ul收起
for(let i = 0; i < len; i++){
var firstAll = $('ul').children().first();
var arr = []; //為綁定.btn的值創(chuàng)建一個數(shù)組
$('li').eq(i).click(function(e){
e.stopPropagation(); //因?yàn)槭录芭輽C(jī)制,一定要注意取消時(shí)間冒泡
if($(this).attr('check')!=="true"){
if($(this).text()=="全部級別"){ //如果當(dāng)前點(diǎn)擊的是“全部級別”,則所有的li背景都改變
$(this).attr('check','true');
$(this).siblings().attr('check',"true");
// arr.push($(this).text());
$('.btn').text($(this).text());
arr = ["一級","二級","三級"];
//此時(shí).btn顯示"全部級別"
}else{
$(this).attr('check','true'); //如果當(dāng)前點(diǎn)擊的li是其他的,則當(dāng)前l(fā)i背景改變
if(arr.includes($(this).text())){
$('.btn').text(arr); //分情況討論此時(shí).btn應(yīng)該如何顯示
}else{ //注意結(jié)合arr
arr.push($(this).text());
$('.btn').text(arr);
}
}
if($(this).text()!=="全部級別"&&firstAll.next().attr('check')=='true'&&firstAll.next().next().attr('check')=='true'&&firstAll.next().next().next().attr('check')=='true'){
$('ul').children().first().attr('check','true');
$('.btn').text($('ul').children().first().text());
} //if判斷語句,我覺得肯定有其他的方法,我這個簡直太簡單粗暴了,可是我還沒想到...
//這是我們應(yīng)該考慮的一種情況,當(dāng)其他幾項(xiàng)全選時(shí),"全部級別"應(yīng)該默認(rèn)被選中
}else{
if($(this).text()=="全部級別"){ //同理,當(dāng)當(dāng)前元素被選中,再被點(diǎn)擊時(shí)要取消選中
$(this).attr('check','false');
$(this).siblings().attr('check',"false");
$('.btn').text($(this).text()); //注意此時(shí),雖然.btn顯示為"全部級別"
arr = []; //但實(shí)際上沒有任何元素被選中,所以arr實(shí)際為空
}else{
$(this).attr('check','false');
$('ul').children().first().attr('check','false');
for(var a = 0 ; a < arr.length; a++){
if(arr[a] == $(this).text()){
arr.splice(a,1); //數(shù)組方法,刪除索引為a的一個元素
$('.btn').text(arr);
if(arr.length == 0){ //如果arr數(shù)據(jù)為空,那么.btn顯示"全部級別"
$('.btn').text(firstAll.text())
}
}
}
}
}
})
}
})
</script>
</body>
</html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun運(yùn)行上述代碼,測試運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery實(shí)現(xiàn)表單驗(yàn)證簡單實(shí)例演示
這篇文章向大家推薦了一個jquery實(shí)現(xiàn)表單驗(yàn)證簡單實(shí)例演示,需要的朋友可以參考下2015-11-11
jQuery中的bind綁定事件與文本框改變事件的臨時(shí)解決方法
暫時(shí)沒有想到什么好的解決辦法,我現(xiàn)在加了個瀏覽器判斷非ie的話就注冊blur事件,這樣有個問題就是blur實(shí)在別的控件活動焦點(diǎn)的時(shí)候,txtStation控件注冊的方法是為了填充它緊挨著的一個下拉列表2010-08-08
在線引用最新jquery文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄诰€引用最新jquery文件的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
基于Bootstrap和JQuery實(shí)現(xiàn)動態(tài)打開和關(guān)閉tab頁的實(shí)例代碼
這篇文章主要介紹了基于Bootstrap和JQuery實(shí)現(xiàn)動態(tài)打開和關(guān)閉tab頁的實(shí)例代碼,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
JQuery+Bootstrap 自定義全屏Loading插件的示例demo
這篇文章主要介紹了JQuery+Bootstrap 自定義全屏Loading插件,本文通過示例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
基于jQuery對象和DOM對象和字符串之間的轉(zhuǎn)化實(shí)例
下面小編就為大家?guī)硪黄趈Query對象和DOM對象和字符串之間的轉(zhuǎn)化實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
jQuery獲取瀏覽器中的分辨率實(shí)現(xiàn)代碼
本篇文章小編為大家介紹,使用jQuery獲取瀏覽器中的分辨率的方法。需要的朋友參考下2013-04-04

