基于jquery實(shí)現(xiàn)多選下拉列表
更新時間:2017年08月02日 09:54:10 作者:wodeboke0219
這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)多選下拉列表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了jquery實(shí)現(xiàn)多選下拉列表展示的具體代碼,供大家參考,具體內(nèi)容如下
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
ul li{
list-style: none;
}
.hide{display: none}
.width150{
width: 150px;
}
.width150 input[type="text"]{
width: 100%;
height: 32px;
border: 1px solid #ccc;
border-radius: 4px;
padding-left: 12px;
}
.width150 ul{
width: 96%;
padding: 0 0 0 20px;
margin: 0;
border: 1px solid #ccc;
}
.width150 li{
padding: 5px;
}
.width150 li+li{
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<form id="form">
<div class="width150">
可多選年份:<input type="text" id="yearInput" placeholder="請選擇年份" readonly>
<ul id="yearId" class="hide">
</ul>
</div>
</form>
</body>
<script type="text/javascript" src="jquery.js"></script>
<script>
(function(){
var str = '';
var arr = {
0 : {name:'2015',id:0},
1 : {name:'2016',id:0},
2 : {name:'2017',id:0}
};
for (let x in arr){
console.info(x);
str += `<li><label><input type="checkbox" value="${arr[x].id}" data-name="${arr[x].name}">${arr[x].name}</label></li>`;
}
$('#yearId').html(str);
})();
$("#yearInput").click(function(){
$(this).attr('placeholder','');
var name = '';
$('#yearId input').each(function () {//循環(huán)遍歷checkbox
var check=$(this).is(':checked');//判斷是否選中
if(check){
name += $(this).attr('data-name')+',';
$(this).attr('name',"yearId");
}else {
$(this).attr('name',"");
}
});
$("#yearInput").val(name.slice(0,-1));//去除最后的逗號
});
$("#yearId").mouseover(function() {
if (!$("#yearId").hasClass('hover')){//類hover在下面用來驗(yàn)證是否需要隱藏下拉,沒有其他用途。
$("#yearId").addClass('hover');
}
}).mouseout(function(){
$("#yearId").removeClass('hover');
});
$(document).on('click',function() {
if (!$("#yearInput").is(":focus") && !$("#yearId").hasClass('hover')) {//如果沒有選中輸入框且下拉不在hover狀態(tài)。
var name = '';
$('#yearId input').each(function () {//遍歷checkbox
var check = $(this).is(':checked');//判斷是否選中
if (check) {
name += $(this).attr('data-name') + ',';
$(this).attr('name', "yearId");
} else {
$(this).attr('name', "");
}
});
$("#yearInput").val(name.slice(0, -1));//去除最后的逗號
$("#yearId").addClass('hide');
} else {
$("#yearId").removeClass('hide');
}
});
</script>
</html>
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- jQuery+Ajax請求本地?cái)?shù)據(jù)加載商品列表頁并跳轉(zhuǎn)詳情頁的實(shí)現(xiàn)方法
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁)
- jQuery實(shí)現(xiàn)Select下拉列表進(jìn)行狀態(tài)選擇功能
- jQuery實(shí)現(xiàn)聯(lián)動下拉列表查詢框
- jquery實(shí)現(xiàn)圖片列表鼠標(biāo)移入微動
- JQuery實(shí)現(xiàn)列表中復(fù)選框全選反選功能封裝(推薦)
- 基于jQuery和Bootstrap框架實(shí)現(xiàn)仿知乎前端動態(tài)列表效果
- 利用jQuery對無序列表排序的簡單方法
- 多功能jQuery樹插件zTree實(shí)現(xiàn)權(quán)限列表簡單實(shí)例
- jQuery EasyUI學(xué)習(xí)教程之datagrid點(diǎn)擊列表頭排序
- jQuery實(shí)現(xiàn)列表的增加和刪除功能
相關(guān)文章
jQuery實(shí)現(xiàn)類似淘寶網(wǎng)圖片放大效果的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)類似淘寶網(wǎng)圖片放大效果的方法,實(shí)例分析了jquery實(shí)現(xiàn)圖片放大效果的方法,涉及jquery操作鼠標(biāo)事件及頁面元素屬性修改的相關(guān)技巧,需要的朋友可以參考下2015-07-07
jQuery中each循環(huán)的跳出和結(jié)束實(shí)例
下面小編就為大家?guī)硪黄猨Query中each循環(huán)的跳出和結(jié)束實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
jquery Form輕松實(shí)現(xiàn)文件上傳
這篇文章主要介紹了jquery Form輕松實(shí)現(xiàn)文件上傳的相關(guān)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
jQuery實(shí)現(xiàn)用戶信息表格的添加和刪除功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)用戶信息表格的添加和刪除功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-09-09
深入理解JQuery keyUp和keyDown的區(qū)別
這篇文章主要是對JQuery中keyUp與keyDown的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12

