以下代码在操作选择框中的选项时工作正常。但是当我把它放在表格中时它不起作用。请帮忙。提前谢谢。
<html>
<head>
<title>Catogery list</title>
<script type="text/javascript">
var catogeries=new Object();
catogeries['Accessories'] = 'Cosmetics/Perfumes|Jewelry|Handbags|Shoes|Watches';
catogeries['Entertainment/Media'] = 'Videos/Movies|Art|Photo|News|Television|Music';
catogeries['Automobile'] = 'Cars| Motorcycles| Tools & Supplies| Accessories';
catogeries['Books & Magazines'] = 'eBooks| Audio Books| Books| Magazines';
catogeries['Business & Career'] = 'Real Estate| Office| Productivity Tools| Employment| B-TO-B| Marketing';
catogeries['Apparel'] = 'Childrens| Mens| Womens';
function subcat(elem)
{
var catarr;
var subb=document.getElementById("subcat");
subb.disabled=false;
subb.length=0;
var cat=elem.options[elem.selectedIndex].text;
if(catogeries[cat])
{
catarr=catogeries[cat].split('|');
subb.options[0]=new Option('select subcat',' ');
for(var i=0; i<catarr.length; i++)
{
subb.options[i+1]=new Option(catarr[i],catarr[i]);
}
}
}
</script>
</head>
<body>
<select id="cat" onChange="subcat(this,subcat)" style="width:200px;">
<option value="" selected="selected">Select Catogery</option>
<option value="">Accessories</option>
<option value="">Entertainment/Media</option>
<option value="">Automobile</option>
<option value="">Books & Magazines</option>
<option value="">Business & Career</option>
<option value="">Apparel</option>
</select>
</br>
</br>
<select id="subcat" style="width:200px; " disabled="true">
</select>
</body>
</html>
答案 0 :(得分:0)
这似乎是一个名称冲突问题。您已将JavaScript函数和Select命名为同名,即 subcat 。将这些中的每一个唯一地命名,以便在解析名称时没有歧义。 e.g。
<html>
<head>
<title>Catogery list</title>
<script type="text/javascript">
var catogeries=new Object();
catogeries['Accessories'] = 'Cosmetics/Perfumes|Jewelry|Handbags|Shoes|Watches';
catogeries['Entertainment/Media'] = 'Videos/Movies|Art|Photo|News|Television|Music';
catogeries['Automobile'] = 'Cars| Motorcycles| Tools & Supplies| Accessories';
catogeries['Books & Magazines'] = 'eBooks| Audio Books| Books| Magazines';
catogeries['Business & Career'] = 'Real Estate| Office| Productivity Tools| Employment| B-TO-B| Marketing';
catogeries['Apparel'] = 'Childrens| Mens| Womens';
function GetSubCategeries(elem)
{
var catarr;
var subb=document.getElementById("subcat");
subb.disabled=false;
subb.length=0;
var cat=elem.options[elem.selectedIndex].text;
if(catogeries[cat])
{
catarr=catogeries[cat].split('|');
subb.options[0]=new Option('select subcat',' ');
for(var i=0; i<catarr.length; i++)
{
subb.options[i+1]=new Option(catarr[i],catarr[i]);
}
}
}
</script>
</head>
<body>
<form>
<select id="cat" onChange="GetSubCategeries(this,subcat)" style="width:200px;">
<option value="" selected="selected">Select Catogery</option>
<option value="">Accessories</option>
<option value="">Entertainment/Media</option>
<option value="">Automobile</option>
<option value="">Books & Magazines</option>
<option value="">Business & Career</option>
<option value="">Apparel</option>
</select>
</br>
</br>
<select id="subcat" style="width:200px; " disabled="true">
</select>
</form>
</body>
</html>
注意我已将选择包含在Form元素中,并将JavaScript函数重命名为 GetSubCategories()。