【发布时间】:2012-06-04 04:10:57
【问题描述】:
我有三个 HTML DropDownList (<select><option></option></select>)。第一个DropDownList 包含类别,第二个包含不同产品的子类别,第三个包含品牌(或制造商)。
选择一个类别后,应根据传递给 Ajax 函数的类别 ID 一次从数据库中填充两个下拉子类别和品牌。我正在使用以下 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) Javascript 函数被调用来执行 Ajax 请求。问题是我需要同时填充子类别和品牌下拉列表(选择类别时)。
它正在工作,并且根据传递的类别 ID 一次填充两个下拉列表(它是上述函数 selected 的参数)。
我不必要地使用了函数getBrandList() 底部的警告框。注释此警告框时,仅填充一个下拉菜单,即子类别。品牌仍然是空的。我不再需要这个警报框了。
为什么会这样?解决办法是什么?
【问题讨论】:
标签: php javascript ajax