三级联动
-1-
//学校 $.ajax({ type: "post", contentType: "application/json", url: "/userinfo/getSPCList.do", data: "{level:1}", success: function(result) { var op = ""; for(var i = 0; i < result.d.length; i++) { op += '<option value=' + result.d[i].schoolID + '>'; op += result.d[i].schoolName; op += '</option>'; } $("#fSchool").append(op); } }); //当学校改变时,专业改变 $('#fSchool').change(function() { $('#fProfessional option:gt(0)').remove(); $('#fClass option:gt(0)').remove(); $.ajax({ type: "post", contentType: "application/json", url: "/userinfo/getSPCList.do", data: "{proid:'" + $(this).val() + "'}", success: function(result) { var op = ''; for(var i = 0; i < result.d.length; i++) { op += '<option value=' + result.d[i].professionID + '>'; op += result.d[i].professionName; op += '</option>'; } $('#fProfessional').append(profession); } }); }); //当专业改变时,班级改变 $('#fClass').change(function() { $.ajax({ type: "post", contentType: "application/json", url: "/useinfo/getClassList.do", data: "{classID:'" + $(this).val() + "'}", success: function(result) { var op = ''; for(var i = 0; i < result.d.length; i++) { op += '<option value=' + result.d[i].classID + '>'; op += result.d[i].className; op += '</option>'; } $('#fClass').append(op); } }); });
--2--
//学校改变时,专业改变 $("#aSchool").change(function() { var thisOne = $(this); var schoolID = $(this).val(); $(this).next().hide(); $(this).next().next().hide(); if(schoolID == "") { $(this).next().html("<option value=''>请选择</option>"); $(this).next().next().html("<option value=''>请选择</option>"); return; } $.post("<c:url value='/userinfo/getSPCList.do'/>", { level: 2, parentID: schoolID, ranNum: Math.random() }, function(data) { var result = eval('(' + data + ')'); if(result.msg == null) { thisOne.next().show().html("<option value=''>请选择</option>"); for(var i = 0; i < result.data.length; i++) { thisOne.next().append("<option value=" + result.data[i].spcID + ">" + result.data[i].spcName + "</option>"); } thisOne.next().css({ visibility: "" }); } else { alert(result.msg); } }); }); //专业改变时,班级改变 $("#aProfession").change(function() { var thisOne = $(this); var professionID = $(this).val(); thisOne.next().hide(); if(professionID == "") { thisOne.next().html("<option value=''>请选择</option>"); return; } $.post("<c:url value='/userinfo/getSPCList.do'/>", { level: 3, parentID: professionID, ranNum: Math.random() }, function(data) { var result = eval('(' + data + ')'); if(result.msg == null) { thisOne.next().show().html("<option value=''>请选择</option>"); for(var i = 0; i < result.data.length; i++) { thisOne.next().append("<option value=" + result.data[i].spcID + ">" + result.data[i].spcName + "</option>"); } thisOne.next().css({ visibility: "" }); } else { alert(result.msg); } }); });
--3--
//学校、专业、班级三级联动 function changeSpc(leavel) { var spcID = 0; if(leavel == 1) { spcID = $("#schoolID").val(); } else if(leavel == 2) { spcID = $("#professionID").val(); } else { alert("数据错误"); return false; } $.getJSON("<c:url value='/spclass/getSpclassListByParent.do'/>", { spcID: spcID, ranNum: Math.random() }, function(result) { if(result.status == 0) { alert(result.msg); } else if(result.status == 1) { if(leavel == 1) { $("#professionID").attr("disabled", false); } else if(leavel == 2) { $("#classID").attr("disabled", false); } if(result.data.length >= 0) { var optionHtml = "<option value='0'>---请选择---</option>"; for(var i = 0; i < result.data.length; i++) { optionHtml += "<option value='" + result.data[i].spcID + "'>" + result.data[i].spcName + "</option>"; } if(leavel == 1) { $("#professionID").html(optionHtml); $("#classID").html("<option value='0'>---请选择---</option>"); } else if(leavel == 2) { $("#classID").html(optionHtml); } } } }); }