下拉選單多選
阿新 • • 發佈:2020-12-22
技術標籤:jquery
下拉選單多選
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.box{width: 500px;height: 500px;margin: 0 auto;font-size: 12px;}
.select_more{width: 100px;height: 20px;position: relative; border-color: #a2bac0 #b8d0d6 #b8d0d6 #a2bac0;border-style: solid;border-width: 1px;cursor: default;}
.select_option_box{position: absolute;left: -1px;top: 19px;width: 100%;z-index: 1;background-color: #FFFFFF;border-color: #a2bac0 #b8d0d6 #b8d0d6 #a2bac0;border-style: solid;border-width: 1px;border-top: none; display: none;cursor: default;}
.select_item{width: 100%;line-height: 20px;padding: 1px 5px;box-sizing: border-box;}
.select_item input,.select_item span{display: inline-block;vertical-align: top;}
.select_item input{margin: 0;margin-right: 3px;margin-top: 3.5px;}
.select_item span{line-height : 23px;}
.select_checked{height: 20px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;}
</style>
<script src="js/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
var checkedIds = [], checkedArr = [];
// 監測崗位名稱選擇
$(".select_item").each(function(e) {
$(this).click(function(e) {
e.stopPropagation(); // 神龍點睛之筆!!!!!
if($(this).find('input').attr('checked')) {
$(this).find('input').removeAttr('checked');
// 刪除
checkedArr.splice(checkedArr.indexOf($(this).find('span').text()), 1);
checkedIds.splice(checkedIds.indexOf($(this).find('input').val()), 1);
$('#selectChecked').text(checkedArr)
$('#checkedStr').val(checkedIds);
if(checkedArr.length == 0) {
$('#selectChecked').text('請選擇')
}
}else {
if($(this).find('span').text() != '請選擇' && $(this).find('span').text()) {
$(this).find('input').attr('checked', 'checked')
//新增
checkedArr.push($(this).find('span').text());
checkedIds.push($(this).find('input').val());
$('#selectChecked').text(checkedArr)
$('#checkedStr').val(checkedIds);
}
}
})
})
//崗位名稱請選擇點選事件
$('#selectChecked').click(function(){
if($('#selectBox').css('display') == 'block') {
$('#selectBox').css('display', 'none');
}else {
$('#selectBox').css('display', 'block');
}
})
// 點選其他地方select消失
$('#body').click(function() {
$('#selectBox').css('display', 'none');
})
})
</script>
</head>
<body id="body">
<div class="box">
<input type="text" name="" id="checkedStr" value="" />
<div class="select_more">
<div class="select_item select_checked" id="selectChecked">請選擇</div>
<div class="select_option_box" id="selectBox">
<div class="select_item">
<input type="checkbox" value="1" /><span>團隊經理</span>
</div>
<div class="select_item">
<input type="checkbox" value="2" /><span>業務管理</span>
</div>
<div class="select_item">
<input type="checkbox" value="3" /><span>綜合事務</span>
</div>
<div class="select_item">
<input type="checkbox" value="4" /><span>授權審批</span>
</div>
</div>
</div>
</div>
</body>
</html>