javascript - 使用 javascript 的级联下拉菜单不起作用

标签 javascript

我有这段 JavaScript 代码,但它不起作用。有人可以检查我的代码吗? 顺便说一句,我刚刚从这里的一个问题中得到了这段代码。谢谢

function CollegeDepartment() {
    var s1 = document.getElementById("college");
    var s2 = document.getElementById("department");
    s2.innerHTML = "";
    if (s1.value == "College of Engineering") {
        var optionArray = ["Civil Engineering", "Computer Engineering", "Electrical Engineering", "Electronics and Communication Engineering, Industrial Engineering, Mechanical Engineering"];
    } else if (s1.value == "CAS") {
        var optionArray = ["Political Science", "Mascomm", "Liacomm"];
    } else if (s1.value == "Commerce") {
        var optionArray = ["Business Ad", "Hotel Management", "Tourism"];
    } else if (s1.value == "Education") {
        var optionArray = ["SPED"];
    } else if (s1.value == "CICCT") {
        var optionArray = ["Computer Science", "Information Technology"];
    }

    for (var option in optionArray) {
        var newOption = document.createElement("option");
        newOption.value = optionArray[option];
        newOption.innerHTML = optionArray[option];
        s2.options.add(newOption);
    }
};

编辑:

HTML

 <select class="form-control" name="college" id="college" runat="server" oninput="CollegeDepartment()">
                 <option selected>Select College</option>
                 <option value="College of Engineering">College of Engineering</option>
                 <option value="CAS">College of Arts and Science</option>
                 <option value="Commerce">College of Commerce</option>
                 <option value="Education">College of Education</option>
                 <option value="CICCT">CICCT</option>   
             </select>
        </div>
            <br />

        <div class="form-group">
            <select id="department" name="department" class="form-control" runat="server" placeholder="Department" >
                <option value="Department" selected>Select Department</option>
            </select>
        </div>

最佳答案

立即发现您的问题,您正在使用 oninput 事件,您需要的是 onchange

所以这个:

 <select class="form-control" name="college" id="college" runat="server" oninput="CollegeDepartment()">

变成:

 <select class="form-control" name="college" id="college" runat="server" onchange="CollegeDepartment()">

function CollegeDepartment() {
    var s1 = document.getElementById("college");
    var s2 = document.getElementById("department");
    s2.innerHTML = "";
    if (s1.value == "College of Engineering") {
        var optionArray = ["Civil Engineering", "Computer Engineering", "Electrical Engineering", "Electronics and Communication Engineering, Industrial Engineering, Mechanical Engineering"];
    } else if (s1.value == "CAS") {
        var optionArray = ["Political Science", "Mascomm", "Liacomm"];
    } else if (s1.value == "Commerce") {
        var optionArray = ["Business Ad", "Hotel Management", "Tourism"];
    } else if (s1.value == "Education") {
        var optionArray = ["SPED"];
    } else if (s1.value == "CICCT") {
        var optionArray = ["Computer Science", "Information Technology"];
    }

    for (var option in optionArray) {
        var newOption = document.createElement("option");
        newOption.value = optionArray[option];
        newOption.innerHTML = optionArray[option];
        s2.options.add(newOption);
    }
};
<select class="form-control" name="college" id="college" runat="server" onchange="CollegeDepartment()">
                 <option selected>Select College</option>
                 <option value="College of Engineering">College of Engineering</option>
                 <option value="CAS">College of Arts and Science</option>
                 <option value="Commerce">College of Commerce</option>
                 <option value="Education">College of Education</option>
                 <option value="CICCT">CICCT</option>   
             </select>
        </div>
            <br />

        <div class="form-group">
            <select id="department" name="department" class="form-control" runat="server" placeholder="Department" >
                <option value="Department" selected>Select Department</option>
            </select>
        </div>

尝试上面的片段,或者这里是一个 CodePen: http://codepen.io/anon/pen/PPpWJp

关于javascript - 使用 javascript 的级联下拉菜单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910522/

相关文章:

javascript - Ajax 调用 Web API

javascript - NodeJS Crypto加密到前端javascript解密

javascript - 从 google 表格连接到 SQL SERVER

javascript - 将ps.js 包含到rails/webpack 项目中

javascript 在基于 Rails 3.1 Assets 的应用程序中包含两次

javascript - 在区分大小写的情况下,通过过滤器搜索并包含

javascript - tinymce - fontsizeselect 格式显示值

javascript - 使用 d3 将多个矩形嵌套在多个 svg 容器中?

javascript - 使用 javascript/jQuery 向元素添加标题的最有效方法

javascript - 排序 HTML 结构