下拉列表的内容从数据库获取,方法写好了。但是ajax不会写。求个代码
function getXmlHttp() {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function getBuildings(){
var campus_id = $('#campusSelect option:selected') .val();
$("#buildingSelect").empty();
$("#roomSelect").empty();
if (typeof(campus_id) == "undefined" || campus_id == "") {
alert("请选择校区!")
return;
}
var xmlhttp = getXmlHttp();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var select = document.getElementById("buildingSelect");
var buildings = $.parseJSON(xmlhttp.responseText);
for(var i = 0; i < buildings.length; i++) {
select.options.add(new Option(buildings[i].building_name,buildings[i].building_id));
}
}
}
xmlhttp.open("GET","/Schedule/json/building/list?campus_id=" + campus_id ,true);
xmlhttp.send();
}
function getRooms() {
var building_id = $('#buildingSelect option:selected') .val();
$("#roomSelect").empty();
if (typeof(building_id) == "undefined" || building_id == "") {
alert("请选择教学楼!")
return;
}
var xmlhttp = getXmlHttp();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var select = document.getElementById("roomSelect");
var rooms = $.parseJSON(xmlhttp.responseText);
for(var i = 0; i < rooms.length; i++) {
select.options.add(new Option(rooms[i].room_name,rooms[i].room_id));
}
}
}
xmlhttp.open("GET","/Schedule/json/room/list?building_id=" + building_id ,true);
xmlhttp.send();
}
//
function getSchedule() {
var Sel_XNXQ = $('#termSelect option:selected') .val();
var Sel_XQ = $('#campusSelect option:selected') .val();
var Sel_JXL = $('#buildingSelect option:selected') .val();
var Sel_ROOM = $('#roomSelect option:selected') .val()
if (typeof(Sel_ROOM) == "undefined" || Sel_ROOM == "") {
alert("请选择需要查询的教室!")
return;
}
var type = $('input:radio[name="type"]:checked').val();
var txt_yzm = $("#verCode").val();
$("#verCode").val("");
$("#roomSchedule").html("");
$("#picLoading").show();
$("#picGif").hide();
var xmlhttp = getXmlHttp();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
$("#picLoading").hide();
var div = document.getElementById("roomSchedule");
var table = $.parseJSON(xmlhttp.responseText);
if (table.schedule_content == "null") {
if ($("#match").is(':hidden')) {
$("#match").show();
}else{
alert("请输入验证码!")
}
}else if(table.schedule_content == "txt_yzm"){
refleshValidate(document.getElementById("imgVerCode"));
alert("验证码错误!");
}else{
div.innerHTML = table.schedule_content;
$("#match").hide();
$("#picGif").hide();
}
}
}
xmlhttp.open("POST","/Schedule/json/room/course" ,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("Sel_XNXQ=" + Sel_XNXQ + "&Sel_XQ=" + Sel_XQ + "&Sel_JXL=" + Sel_JXL + "&Sel_ROOM=" + Sel_ROOM + "&type=" + type + "&txt_yzm=" + txt_yzm);
}
之前写的下拉列表三级联动的,第一个方法是获取不同浏览器下可以使用的ajax,下面三个方法就是三级联动,先选择校区,根据校区查找教学里,然后选择教学楼,在根据教学楼查教室,最后一个方法是根据教室查找课表信息。