基本上我想加载两个国家列表和initializePage()在body onload被调用的组合框。
下面的代码是从一个js文件中复制出来的。我的问题是:一个组合框加载是正确的,但是当我一起调用两个函数时,总是执行最后调用的组合框加载(换句话说,总是加载一个组合框而不是两个)
my new to AJAX, Please Help
var xmlHttp;
function initializePage()
{
displayCountryFrom();
displayCountryTo();
}
function displayCountryFrom()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return
}
var url="loadCountry.php";
url=url+"?sid="+Math.random();
xmlHttp.onreadystatechange=CountryFromstateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function displayCountryTo()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return
}
var url="loadCountry.php";
url=url+"?sid="+Math.random();
xmlHttp.onreadystatechange=CountryTostateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function CountryFromstateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById( "countryListFrom").innerHTML=
'<option value="0">------Select Coutry1------</option>'+ xmlHttp.responseText;
}
}
function CountryTostateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById( "countryListTo").innerHTML=
'<option value="0">------Select Coutry2------</option>'+ xmlHttp.responseText;
}
}
我认为你应该尝试对这两个请求有不同的变量。现在你只有一个:var xmlHttp
。您必须记住,displayCountryTo()
在启动之前不会等待displayCountryFrom()
结果从服务器返回。因此,让它们共享请求对象是不好的。