使用javascript的级联下拉不起作用

时间:2015-10-02 15:20:49

标签: 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>

1 个答案:

答案 0 :(得分:1)

立即找到您的问题,您使用的是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()">

&#13;
&#13;
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);
    }
};
&#13;
<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>
&#13;
&#13;
&#13;

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