本文實(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}
.width250{
width: 150px;
}
.width250 input[type="text"]{
width: 100%;
height: 32px;
border: 1px solid #ccc;
border-radius: 4px;
padding-left: 12px;
}
.width250 ul{
width: 96%;
padding: 0 0 0 20px;
margin: 0;
border: 1px solid #ccc;
}
.width250 li{
padding: 5px;
}
.width250 li+li{
border-top: 1px solid #ccc;
}
</style>
</head>
<body>
<form id="form">
<div class="width250">
可多選年份:<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));//去除最后的逗號(hào)
});
$("#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));//去除最后的逗號(hào)
$("#yearId").addClass('hide');
} else {
$("#yearId").removeClass('hide');
}
});
</script>
</html>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站標(biāo)題:基于jquery實(shí)現(xiàn)多選下拉列表-創(chuàng)新互聯(lián)
URL鏈接:http://chinadenli.net/article4/ddpsie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、標(biāo)簽優(yōu)化、網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站、微信小程序、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容