起動時にjavascriptから選択ボックスに入力しようとしていますが、それぞれが前のものに依存しています。
私が持っているHTMLコードで
<body onload="start_up()">
<span id="FirstList">xxxxxxx</span>
<span id="SecondList">xxxxxxx</span>
JavaScriptコードは
function start_up()
{
load_select('','Type1')
load_select(document.getElementById('select_first').value,'Type2')
}
function load_select(argument,code)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getdropdown.php";
url=url+"?q="+code+argument;
url=url+"&sid="+Math.random(); // this is needed to make sure its not loading a cached version
last_http_type=code;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
if (last_http_type=="Type1")
document.getElementById("FirstList").innerHTML=xmlhttp.responseText;
else if (last_http_type=="Type2")
document.getElementById("SecondList").innerHTML=xmlhttp.responseText;
else if (last_http_type=="Type3")
document.getElementById("OutputTable").innerHTML=xmlhttp.responseText;
else if (last_http_type=="Type4")
document.getElementById("OutputTable").innerHTML=xmlhttp.responseText;
last_http_type="cleared";
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
return new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
return null;
}
選択ボックスは、コードが php で生成されるときに select_first および select_second という名前が付けられます。
これは最初のものでは問題なく動作しますが、2 番目の load_select は select_first を認識していないために失敗します。私はそれが順番に行われたと仮定していたので、ステートメントに到達するまでに、最初のドロップダウンについて知っているはずです。(上記のコードは、問題を説明するために少し単純化されていることに注意してください。load_select の 2 番目の引数は、正確な SQL 呼び出しを決定します。また、どの load_select が呼び出されたかを知る必要があるため、stateChanged は少し複雑です。しかし、それらはすべて、それ自体、起動時の複数のロードが失敗します。)