私は3つのHTML DropDownList
(<select><option></option></select>
)を持っています。1DropDownList
つ目はカテゴリを含み、2つ目はさまざまな製品のサブカテゴリを含み、3つ目はブランド(またはメーカー)を含みます。
カテゴリを選択すると、Ajax関数に渡されるカテゴリIDに従って、2つのドロップダウンサブカテゴリとブランドがデータベースから一度に入力される必要があります。次のAjaxコードを使用しています。
function ajax()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
}
}
function getBrandList(selected) //selected is the category id.
{
ajax();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("brandList").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax/BrandAjax.php?selected="+selected, true);
xmlhttp.send();
alert(selected);
}
function getSubcategoryList(selected) //selected is the category id.
{
getBrandList(selected); //First above function is invoked to populate brands.
ajax();
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("subCategoryList").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax/SubCatAjax.php?selected="+selected, true);
xmlhttp.send();
}
カテゴリが選択さgetSubcategoryList(selected)
れると、Ajaxリクエストを実行するJavascript関数が呼び出されます。問題は、サブカテゴリとブランドドロップダウンの両方を一度に入力する必要があることです(カテゴリが選択されている場合)。
これは機能しており、渡されたカテゴリID(上記の関数のパラメーター)に従って、両方のドロップダウンに一度に入力されますselected
。
関数の下部にあるアラートボックスを不必要に使用していますgetBrandList()
。このアラートボックスにコメントを付けると、サブカテゴリであるドロップダウンが1つだけ表示されます。ブランドは空のままです。このアラートボックスはもう必要ありません。
なぜこれが起こるのですか?解決策は何ですか?