基于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="請(qǐng)選擇年份" 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));//去除最后的逗號(hào)
});
$("#yearId").mouseover(function() {
if (!$("#yearId").hasClass('hover')){//類hover在下面用來(lái)驗(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));//去除最后的逗號(hào)
$("#yearId").addClass('hide');
} else {
$("#yearId").removeClass('hide');
}
});
</script>
</html>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery+Ajax請(qǐng)求本地?cái)?shù)據(jù)加載商品列表頁(yè)并跳轉(zhuǎn)詳情頁(yè)的實(shí)現(xiàn)方法
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁(yè))
- jQuery實(shí)現(xiàn)Select下拉列表進(jìn)行狀態(tài)選擇功能
- jQuery實(shí)現(xiàn)聯(lián)動(dòng)下拉列表查詢框
- jquery實(shí)現(xiàn)圖片列表鼠標(biāo)移入微動(dòng)
- JQuery實(shí)現(xiàn)列表中復(fù)選框全選反選功能封裝(推薦)
- 基于jQuery和Bootstrap框架實(shí)現(xiàn)仿知乎前端動(dòng)態(tài)列表效果
- 利用jQuery對(duì)無(wú)序列表排序的簡(jiǎn)單方法
- 多功能jQuery樹插件zTree實(shí)現(xiàn)權(quán)限列表簡(jiǎn)單實(shí)例
- jQuery EasyUI學(xué)習(xí)教程之datagrid點(diǎn)擊列表頭排序
- jQuery實(shí)現(xiàn)列表的增加和刪除功能
相關(guān)文章
jquery 注意事項(xiàng)與常用語(yǔ)法小結(jié)
jquery 注意事項(xiàng)與常用語(yǔ)法小結(jié),學(xué)習(xí)jquery的朋友最好收藏下。2010-06-06
JQuery獲取或設(shè)置ckeditor的數(shù)據(jù)(示例代碼)
JQuery獲取或設(shè)置ckeditor的數(shù)據(jù)(示例代碼)。需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
jQuery網(wǎng)頁(yè)選項(xiàng)卡插件rTabs用法實(shí)例分析
這篇文章主要介紹了jQuery網(wǎng)頁(yè)選項(xiàng)卡插件rTabs用法,較為詳細(xì)的分析了jquery選項(xiàng)卡插件rTabs的定義、實(shí)現(xiàn)及使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jquery上傳插件fineuploader上傳文件使用方法(jquery圖片上傳插件)
這篇文章主要介紹了jquery插件fineuploader上傳文件很用方法2013-12-12
Jquery實(shí)現(xiàn)自定義tooltip示例代碼
這篇文章主要介紹了Jquery實(shí)現(xiàn)自定義tooltip的方法,需要的朋友可以參考下2014-02-02
jquery text,radio,checkbox,select操作實(shí)現(xiàn)代碼
學(xué)習(xí)jquery的朋友看參考下,對(duì)form等文本框的一些控制實(shí)現(xiàn)代碼。2009-07-07
jQuery 調(diào)用WebService 實(shí)例講解
以前都是Web程序調(diào)用WebService,.net把WebService封裝的太簡(jiǎn)單了,所以之前都是很膚淺的用,都沒有想過更深層的東西,并且只是停留在表面的添加引用和調(diào)用。2016-06-06

