ajax 级联操作

操作院系--专业级联 此次级联由大到小操作

        var departmentId = $("#departmentId").val();
var majorId = $("#majorId").val();
var basePath = $("#basePath").val();
var dataMajor = "";


$.ajax({
type : "POST", // 用POST方式传输
dataType : "json", // 数据格式:JSON
url : basePath+'department/getDepartmentPage.action', // 目标地址
success : function(data) {
$("#departmentId option").remove();
var datas = eval(data);
// 遍历查询到的数据,输入前台
loadXyDepartment(datas);//遍历院系

ajaxMajorInfo();//加载专业
},
error : function() {
var errorcode = "暂无院系";
}
});

/**
* 遍历院系信息
*/
function loadXyDepartment(data){
$.each(data, function(i, n) {
var head = "";
var html = "";
if(departmentId == n.departmentId){
centre = "' selected = 'selected'>";
}else{
centre = "'>";
}
html += head + n.departmentId + centre + n.departmentName + tail;
$("#departmentId").append(html);
});
}

$("#departmentId").change(function(){
loadXyMajor(dataMajor)
})

/**
* 加载专业信息
*/
function ajaxMajorInfo(){
$.ajax({
type : "POST", // 用POST方式传输
dataType : "json", // 数据格式:JSON
url : basePath+'major/getMajorPage.action', // 目标地址
success : function(data) {
dataMajor = eval(data);
// 遍历查询到的数据,输入前台
loadXyMajor(dataMajor);
},
error : function() {
var errorcode = "暂无专业";
}
});
}

/**
* 遍历专业信息
*/
function loadXyMajor(data){
$("#majorId option").remove();
departmentId = $("#departmentId").val();
$.each(data, function(i, n) {
var head = "";
var html = "";
if(majorId == n.majorId){
centre = "' selected = 'selected'>";
}else{
centre = "'>";
}
if(departmentId == n.departmentId){
html += head + n.majorId + centre + n.majorName + tail;
}

$("#majorId").append(html);
});
}

此次框架使用ssh框架

你可能感兴趣的:(jQuery,Java基础,js,ajax)