我更喜欢通过 Ajax 和 JQuery 来实现,如下所示:
function getClassList(elem)
{
var contentRequests, contentarr; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
contentRequests = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
contentRequests = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
contentRequests = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
contentRequests.onreadystatechange = function()
{
if(contentRequests.readyState == 4&& contentRequests.status==200)
{
document.getElementById(elem).innerHTML = contentRequests.responseText;
}
}
var urltofetch="index.php?methodname=getclasses"
contentRequests.open("GET", urltofetch, true);
contentRequests.send(null);
}
function getStudentList(classelem,elem)
{
var classToFetch = classelem + " option:selected";
var contentRequests, contentarr; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
contentRequests = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
contentRequests = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
contentRequests = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
contentRequests.onreadystatechange = function()
{
if(contentRequests.readyState == 4&& contentRequests.status==200)
{
document.getElementById(elem).innerHTML = contentRequests.responseText;
}
}
var classname = $(classToFetch).text();
alert(classname);
var urltofetch="index.php?methodname=getstudents&cname="+classname
contentRequests.open("GET", urltofetch, true);
contentRequests.send(null);
}