<select name="category" onChange="SelectSubcategory();" style="width:250px" class="required"></select>
子类别
<select id="subcategory" name="subcategory" style="width:250px" class="required"></select>
您好我的网页除了其中一个下拉菜单外。当我选择类别时,子类别不会触发。
function SelectSubcategory(){
// ON selection of category this function should fire
removeAllOptions(document.drop_list.subcategory);
addOption(document.drop_list.subcategory, "", "", "");
if(document.drop_list.category.value =="Hotel"){
addOption(document.drop_list.subcategory, "Room only", "Room only" ) ;
addOption(document.drop_list.subcategory, "Breakfast", "Breakfast" ) ;
addOption(document.drop_list.subcategory, "Breakfast & Evening", "Breakfast & Evening");
if(document.drop_list.category.value =="Chalet"){
addOption(document.drop_list.subcategory, "Chalet only", "Chalet only" ) ;
addOption(document.drop_list.subcategory, "Breakfast", "Breakfast" ) ;
addOption(document.drop_list.subcategory, "Breakfast & Carvery", "Breakfast & Carvery");
}
有人建议使用一个好的调试工具,因为我是编码和Web开发的新手,并且发现错误检查是最耗时的方面。非常感谢
答案 0 :(得分:0)
而不是document.drop_list - 使用document.getElementByName('category')和document.getElementByName('subcategory')
我会修改这样的功能(看起来你也错过了一些括号:
function SelectSubcategory(){
// ON selection of category this function should fire
var cetegory = document.getElementByName('category');
var subcategory= document.getElementByName('subcategory');
removeAllOptions(subcategory);
addOption(subcategory, "", "", "");
if(category.value =="Hotel"){
addOption(subcategory, "Room only", "Room only" ) ;
addOption(subcategory, "Breakfast", "Breakfast" ) ;
addOption(subcategory, "Breakfast & Evening", "Breakfast & Evening");
}
if(category.value =="Chalet"){
addOption(subcategory, "Chalet only", "Chalet only" ) ;
addOption(subcategory, "Breakfast", "Breakfast" ) ;
addOption(subcategory, "Breakfast & Carvery", "Breakfast & Carvery");
}
}
我还必须看到addOption和removeAllOptions函数以确保它们是正确的。
老实说,使用jquery可以轻松完成。