select2多选下拉框
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/css/select2.min.css"/>
<style type="text/css">
#color{
width: 300px;
}
</style>
</head>
<body>
<select name="" id="color" multiple="multiple">
<option value="">请选择颜色</option>
<option value="red">红色</option>
<option value="green">绿色</option>
<option value="blue">蓝色</option>
<option value="yellow">黄色</option>
</select>
<button id="get_value">获取选中的值</button>
<button id="pull_value">红色和蓝色选中</button>
<button id="zero_value">清空值</button>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/js/select2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//初始化select2
$("#color").select2();
//获取选中的值
$("#get_value").click(function(){
var color_value = $("#color").val();
console.log(color_value);
})
// 给多选框赋值
$("#pull_value").click(function(){
$("#color").val(["red","blue"]).trigger("change");
})
// 清空
$("#zero_value").click(function(){
$("#color").val([]).trigger("change");
})
</script>
</html>
本文来自博客园,作者:.net&new,转载请注明原文链接:https://www.cnblogs.com/wugh8726254/p/17464566.html