本文链接:https://blog.csdn.net/ting_125/article/details/79282029
需要做个二级联动的功能,第二级要求是多选,用的multiple-Select方法
以老师-学生为例,数据是从后台传到前台页面的一个list中带list的JSONArray
引用(不要忘了引用图片,要不输入框右边没有三角)
<script src="js/multiple-select.js"></script>
<link rel="stylesheet" href="css/multiple-select.css" />
jsp页面
<div>
<select name="teacher" οnchange="teacherChange()" >
<option value="" selected="selected">未选择</option>
<c:if test="${teachers != null}">
<c:forEach items="${teachers}" var="teacher">
<option value="${teacher.id}">${teacher.name}</option>
</c:forEach>
</c:if>
</select>
</div>
<div>
<select id="student" multiple="multiple"></select>
</div>
js页面
<script type="text/javascript">
var teachers = ${teachers};
$(function(){
$('#student').multipleSelect({
width : 120,
allSelected: '全部', //option全部选了之后框里显示的内容
multiple : false, //是否在一行中显示多个选项
selectAll: false, //是否显示全选复选框
placeholder : '未选择', //默认值
});
});
//改变teacher 切换student
function teacherChange() {
var teacherId = $("[name='teacher']").val();
var students = [];
if (teacherId == null || teacherId == "") {
$('#student').html("");
$('#student').multipleSelect("refresh"); //这步很重要
} else {
$(teachers).each(function(index, teacher) {
if (teacher['id'] == teacherId) {
students = teacher['student'];
return false;
}
});
var r = [];
$(students).each(function(index, student) {
r.push('<option value="'+ student['id'] +'">'+ student['name'] +'</option>');
});
$('#student').html(r.join('\n'));
$('#student').multipleSelect("refresh"); //这步很重要
//因为是通过jquery手动更新option选项,所以要通过refresh方法来重新加载Multiple Select
}
}
</script>
相关推荐
特别申明:本站的主旨在于收集互联网运营相关的干货知识,给运营小伙伴提供便利。
网站所收集到的公开内容均来自于互联网或用户投稿,并不代表本站认同其观点,
也不对网站内容的真实性负责,如有侵权,请联系站长删除